首页 > 其他分享 >四剑客_正则

四剑客_正则

时间:2024-09-09 20:47:40浏览次数:1  
标签:rw Sep -- rpm 09 正则 剑客 root

1 四剑客

1.1 概述

1.2 find命令基本用法

1.2.1 找出/etc/目录下面以.conf结尾的文件⭐⭐⭐⭐⭐
[root@Kylin-V10-sp3 ~/test]# find /etc/ -type f -name *.conf | head -5
/etc/resolv.conf
/etc/dnf/protected.d/systemd.conf
/etc/dnf/protected.d/sudo.conf
/etc/dnf/protected.d/dnf.conf
/etc/dnf/protected.d/yum.conf
1.2.2 找出/etc/目录下面以.conf结尾的文件文件大小大于10k⭐⭐⭐⭐
[root@Kylin-V10-sp3 ~/test]# find /etc/ -type f -name *.conf -size +10k | head -5
/etc/lvm/lvm.conf
/etc/httpd/conf/httpd.conf
/etc/asciidoc/asciidoc.conf
/etc/asciidoc/docbook45.conf
/etc/asciidoc/docbook5.conf
1.2.3 找出/var/log下面以.log结尾的文件并且修改时间大于3天⭐⭐⭐⭐⭐
[root@Kylin-V10-sp3 ~/test]# find /etc/ -type f -name *.conf -mtime +3 | head -5
/etc/dnf/protected.d/systemd.conf
/etc/dnf/protected.d/sudo.conf
/etc/dnf/protected.d/dnf.conf
/etc/dnf/protected.d/yum.conf
/etc/dnf/plugins/copr.conf
1.2.4 查找文件或目录的时候不区分大小写⭐⭐⭐⭐⭐
[root@Kylin-V10-sp3 ~/test]# find /etc/ -iname *.conf | head -5  
/etc/resolv.conf
/etc/dnf/protected.d/systemd.conf
/etc/dnf/protected.d/sudo.conf
/etc/dnf/protected.d/dnf.conf
/etc/dnf/protected.d/yum.conf
1.2.5 根据深度查找文件
[root@Kylin-V10-sp3 ~/test]# find /etc/ -maxdepth 2 -type f -size +20k -name *.conf -mtime +3 | head -5
/etc/lvm/lvm.conf
/etc/asciidoc/docbook45.conf
/etc/asciidoc/docbook5.conf
/etc/asciidoc/html5.conf
/etc/asciidoc/xhtml11.conf
[root@Kylin-V10-sp3 ~/test]# 

1.3 find与其他命令配合

1.3.1 find找出文件后进行删除 ⭐⭐⭐⭐⭐
# 创建测试环境
[root@Kylin-V10-sp3 ~/test]# pwd
/root/test
[root@Kylin-V10-sp3 ~/test]# touch {01..10}.txt
[root@Kylin-V10-sp3 ~/test]# ll
total 8
-rw-r--r-- 1 root root    0 Sep  9 08:59 01.txt
-rw-r--r-- 1 root root    0 Sep  9 08:59 02.txt
-rw-r--r-- 1 root root    0 Sep  9 08:59 03.txt
-rw-r--r-- 1 root root    0 Sep  9 08:59 04.txt
-rw-r--r-- 1 root root    0 Sep  9 08:59 05.txt
-rw-r--r-- 1 root root    0 Sep  9 08:59 06.txt
-rw-r--r-- 1 root root    0 Sep  9 08:59 07.txt
-rw-r--r-- 1 root root    0 Sep  9 08:59 08.txt
-rw-r--r-- 1 root root    0 Sep  9 08:59 09.txt
-rw-r--r-- 1 root root    0 Sep  9 08:59 10.txt
[root@Kylin-V10-sp3 ~/test]# 

# 方法01: find与反引号 ⭐ ⭐ ⭐ ⭐ ⭐
[root@Kylin-V10-sp3 ~/test]# rm -f `find /root/test/ -type f -name '*.txt' `
[root@Kylin-V10-sp3 ~/test]# 
[root@Kylin-V10-sp3 ~/test]# ll
total 4
-rw-r--r-- 1 root root 1220 Sep  8 07:48 msg.log
[root@Kylin-V10-sp3 ~/test]# 

# 方法02: find 管道 ⭐ ⭐ ⭐ ⭐ ⭐
'''
| 与|xargs 区别
|传递的是字符串,文字符号
|xargs 传递是参数 命令后面文件,目录
'''
[root@Kylin-V10-sp3 ~/test]# touch {01..10}.txt
[root@Kylin-V10-sp3 ~/test]#   
[root@Kylin-V10-sp3 ~/test]# find /root/test/ -type f -name '*.txt' | xargs rm -f 
[root@Kylin-V10-sp3 ~/test]# 
[root@Kylin-V10-sp3 ~/test]# ll
total 4
-rw-r--r-- 1 root root 1220 Sep  8 07:48 msg.log
[root@Kylin-V10-sp3 ~/test]# 

# 方法03: find选项 -exec
'''
-exec 命令 {} \;
{} 前面find找出的文件内容
\; 结尾标记.
{} +   + 先执行前面命令执行完成,结果一次性通exec传递给后面命令.
'''
[root@Kylin-V10-sp3 ~/test]# touch {01..10}.txt
[root@Kylin-V10-sp3 ~/test]# 
[root@Kylin-V10-sp3 ~/test]# ll
total 4
-rw-r--r-- 1 root root    0 Sep  9 09:06 01.txt
-rw-r--r-- 1 root root    0 Sep  9 09:06 02.txt
-rw-r--r-- 1 root root    0 Sep  9 09:06 03.txt
-rw-r--r-- 1 root root    0 Sep  9 09:06 04.txt
-rw-r--r-- 1 root root    0 Sep  9 09:06 05.txt
-rw-r--r-- 1 root root    0 Sep  9 09:06 06.txt
-rw-r--r-- 1 root root    0 Sep  9 09:06 07.txt
-rw-r--r-- 1 root root    0 Sep  9 09:06 08.txt
-rw-r--r-- 1 root root    0 Sep  9 09:06 09.txt
-rw-r--r-- 1 root root    0 Sep  9 09:06 10.txt
-rw-r--r-- 1 root root 1220 Sep  8 07:48 msg.log
[root@Kylin-V10-sp3 ~/test]# 
[root@Kylin-V10-sp3 ~/test]# find /root/test/ -type f -name '*.txt' -exec rm -f {} +
[root@Kylin-V10-sp3 ~/test]# 
[root@Kylin-V10-sp3 ~/test]# ll
total 4
-rw-r--r-- 1 root root 1220 Sep  8 07:48 msg.log
[root@Kylin-V10-sp3 ~/test]# 

1.3.2 找出/etc/下以.conf结尾的文件与打包压缩/backup/ ⭐⭐⭐⭐⭐
# 方法01:find+反引号⭐ ⭐ ⭐ ⭐ ⭐
[root@Kylin-V10-sp3 /backup]# tar zcvf /backup/`date +%F_%w`.tar.gz `find /etc/ -type f -name '*.conf'`
# 检查压缩包
[root@Kylin-V10-sp3 /backup]# tar tf 2024-09-09_1.tar.gz

# 方法02:find |xargs⭐ ⭐ ⭐ ⭐ ⭐
[root@Kylin-V10-sp3 /backup]# find /etc/ -type f -name '*.conf' | xargs tar zcvf /backup/`date +%F_%w`.tar.gz
[root@Kylin-V10-sp3 /backup]# ll
total 244
-rw-r--r-- 1 root root 151626 Sep  9 09:28 2024-09-09_1.tar.gz
-rw-r--r-- 1 root root  92160 Sep  8 06:45 etc_conf.tar.gz

# 方法03: find -exec

'''
有坑,用 {} \; 发现打包压缩后只有1个文件.
find 与-exe执行流程
find找出1个文件 exec执行1次命令
{} + + 先执行前面命令执行完成,结果一次性通exec传递给后面命令. 
'''

[root@Kylin-V10-sp3 /backup]# find /etc/ -type f -name '*.conf' -exec tar zcvf /backup/`date +%F_%w`.tar.gz {} +
1.3.3 find命令与cp/mv
'''
背景:
开启yum缓存
vim /etc/yum.conf
keepcache=1 #开启缓存软件包功能
缓存/var/cache/yum目录 以.rpm结尾.

find与|xargs传参
cp /backup/rpms/ 参数 ... . .. . . .
cp 文件 目录 目标(目录)
cp -t 目标(目录) 文件 目录
'''
#需求:把/var/cache/yum目录 以.rpm结尾,复制/移动到指定的目录/backup/rpms/

方法01: find+反引号
[root@Kylin-V10-sp3 /backup/rpms]# cp `find /var/cache/yum/ -type f -name *.rpm` /backup/rpms/
[root@Kylin-V10-sp3 /backup/rpms]# ll
total 9068
-rw-r--r-- 1 root root  140704 Sep  9 09:41 createrepo_c-0.16.0-3.p01.ky10.x86_64.rpm
-rw-r--r-- 1 root root   60128 Sep  9 09:41 drpm-0.5.0-1.ky10.x86_64.rpm
-rw-r--r-- 1 root root  271828 Sep  9 09:41 gperftools-libs-2.8-1.ky10.x86_64.rpm
-rw-r--r-- 1 root root   56364 Sep  9 09:41 libunwind-1.3.1-3.ky10.x86_64.rpm
-rw-r--r-- 1 root root   83736 Sep  9 09:41 lrzsz-0.12.20-46.ky10.x86_64.rpm
-rw-r--r-- 1 root root  505932 Sep  9 09:41 nginx-1.21.5-2.p02.ky10.x86_64.rpm
-rw-r--r-- 1 root root    8248 Sep  9 09:41 nginx-all-modules-1.21.5-2.p02.ky10.noarch.rpm
-rw-r--r-- 1 root root    9288 Sep  9 09:41 nginx-filesystem-1.21.5-2.p02.ky10.noarch.rpm
-rw-r--r-- 1 root root   17808 Sep  9 09:41 nginx-mod-http-image-filter-1.21.5-2.p02.ky10.x86_64.rpm
-rw-r--r-- 1 root root   27480 Sep  9 09:41 nginx-mod-http-perl-1.21.5-2.p02.ky10.x86_64.rpm
-rw-r--r-- 1 root root   16504 Sep  9 09:41 nginx-mod-http-xslt-filter-1.21.5-2.p02.ky10.x86_64.rpm
-rw-r--r-- 1 root root   49608 Sep  9 09:41 nginx-mod-mail-1.21.5-2.p02.ky10.x86_64.rpm
-rw-r--r-- 1 root root   72068 Sep  9 09:41 nginx-mod-stream-1.21.5-2.p02.ky10.x86_64.rpm
-rw-r--r-- 1 root root 6146064 Sep  9 09:41 nmap-7.92-1.p01.ky10.x86_64.rpm
-rw-r--r-- 1 root root   64340 Sep  9 09:41 telnet-0.17-77.ky10.x86_64.rpm
-rw-r--r-- 1 root root   52192 Sep  9 09:41 tree-1.8.0-2.ky10.x86_64.rpm
-rw-r--r-- 1 root root 1664616 Sep  9 09:41 vim-enhanced-9.0-19.p03.ky10.x86_64.rpm
[root@Kylin-V10-sp3 /backup/rpms]# 

方法02:find+|xargs
[root@Kylin-V10-sp3 /backup/rpms]# find /var/cache/yum/ -type f -name *.rpm | xargs cp -t /backup/rpms/
[root@Kylin-V10-sp3 /backup/rpms]# 
[root@Kylin-V10-sp3 /backup/rpms]# ll
total 9068
-rw-r--r-- 1 root root  140704 Sep  9 09:42 createrepo_c-0.16.0-3.p01.ky10.x86_64.rpm
-rw-r--r-- 1 root root   60128 Sep  9 09:42 drpm-0.5.0-1.ky10.x86_64.rpm
-rw-r--r-- 1 root root  271828 Sep  9 09:42 gperftools-libs-2.8-1.ky10.x86_64.rpm
-rw-r--r-- 1 root root   56364 Sep  9 09:42 libunwind-1.3.1-3.ky10.x86_64.rpm
-rw-r--r-- 1 root root   83736 Sep  9 09:42 lrzsz-0.12.20-46.ky10.x86_64.rpm
-rw-r--r-- 1 root root  505932 Sep  9 09:42 nginx-1.21.5-2.p02.ky10.x86_64.rpm
-rw-r--r-- 1 root root    8248 Sep  9 09:42 nginx-all-modules-1.21.5-2.p02.ky10.noarch.rpm
-rw-r--r-- 1 root root    9288 Sep  9 09:42 nginx-filesystem-1.21.5-2.p02.ky10.noarch.rpm
-rw-r--r-- 1 root root   17808 Sep  9 09:42 nginx-mod-http-image-filter-1.21.5-2.p02.ky10.x86_64.rpm
-rw-r--r-- 1 root root   27480 Sep  9 09:42 nginx-mod-http-perl-1.21.5-2.p02.ky10.x86_64.rpm
-rw-r--r-- 1 root root   16504 Sep  9 09:42 nginx-mod-http-xslt-filter-1.21.5-2.p02.ky10.x86_64.rpm
-rw-r--r-- 1 root root   49608 Sep  9 09:42 nginx-mod-mail-1.21.5-2.p02.ky10.x86_64.rpm
-rw-r--r-- 1 root root   72068 Sep  9 09:42 nginx-mod-stream-1.21.5-2.p02.ky10.x86_64.rpm
-rw-r--r-- 1 root root 6146064 Sep  9 09:42 nmap-7.92-1.p01.ky10.x86_64.rpm
-rw-r--r-- 1 root root   64340 Sep  9 09:42 telnet-0.17-77.ky10.x86_64.rpm
-rw-r--r-- 1 root root   52192 Sep  9 09:42 tree-1.8.0-2.ky10.x86_64.rpm
-rw-r--r-- 1 root root 1664616 Sep  9 09:42 vim-enhanced-9.0-19.p03.ky10.x86_64.rpm
[root@Kylin-V10-sp3 /backup/rpms]#

方法03:find+ exec
[root@Kylin-V10-sp3 /backup/rpms]# rm -f *.rpm
[root@Kylin-V10-sp3 /backup/rpms]# 
[root@Kylin-V10-sp3 /backup/rpms]# ll
total 0
[root@Kylin-V10-sp3 /backup/rpms]# 
[root@Kylin-V10-sp3 /backup/rpms]# find /var/cache/yum/ -type f -name *.rpm -exec cp -t /backup/rpms/ {} +
[root@Kylin-V10-sp3 /backup/rpms]# 
[root@Kylin-V10-sp3 /backup/rpms]# ll
total 9068
-rw-r--r-- 1 root root  140704 Sep  9 09:44 createrepo_c-0.16.0-3.p01.ky10.x86_64.rpm
-rw-r--r-- 1 root root   60128 Sep  9 09:44 drpm-0.5.0-1.ky10.x86_64.rpm
-rw-r--r-- 1 root root  271828 Sep  9 09:44 gperftools-libs-2.8-1.ky10.x86_64.rpm
-rw-r--r-- 1 root root   56364 Sep  9 09:44 libunwind-1.3.1-3.ky10.x86_64.rpm
-rw-r--r-- 1 root root   83736 Sep  9 09:44 lrzsz-0.12.20-46.ky10.x86_64.rpm
-rw-r--r-- 1 root root  505932 Sep  9 09:44 nginx-1.21.5-2.p02.ky10.x86_64.rpm
-rw-r--r-- 1 root root    8248 Sep  9 09:44 nginx-all-modules-1.21.5-2.p02.ky10.noarch.rpm
-rw-r--r-- 1 root root    9288 Sep  9 09:44 nginx-filesystem-1.21.5-2.p02.ky10.noarch.rpm
-rw-r--r-- 1 root root   17808 Sep  9 09:44 nginx-mod-http-image-filter-1.21.5-2.p02.ky10.x86_64.rpm
-rw-r--r-- 1 root root   27480 Sep  9 09:44 nginx-mod-http-perl-1.21.5-2.p02.ky10.x86_64.rpm
-rw-r--r-- 1 root root   16504 Sep  9 09:44 nginx-mod-http-xslt-filter-1.21.5-2.p02.ky10.x86_64.rpm
-rw-r--r-- 1 root root   49608 Sep  9 09:44 nginx-mod-mail-1.21.5-2.p02.ky10.x86_64.rpm
-rw-r--r-- 1 root root   72068 Sep  9 09:44 nginx-mod-stream-1.21.5-2.p02.ky10.x86_64.rpm
-rw-r--r-- 1 root root 6146064 Sep  9 09:44 nmap-7.92-1.p01.ky10.x86_64.rpm
-rw-r--r-- 1 root root   64340 Sep  9 09:44 telnet-0.17-77.ky10.x86_64.rpm
-rw-r--r-- 1 root root   52192 Sep  9 09:44 tree-1.8.0-2.ky10.x86_64.rpm
-rw-r--r-- 1 root root 1664616 Sep  9 09:44 vim-enhanced-9.0-19.p03.ky10.x86_64.rpm
[root@Kylin-V10-sp3 /backup/rpms]# 
[root@Kylin-V10-sp3 /backup/rpms]# 

1.4 特殊符号之引号系列

四剑客命令单独记忆.

1.5 特殊符号之重定向符号系列

2 正则

2.1 为何使用正则

2.2 正则分类

2.3 基础正则

2.4 扩展正则

2.5 正则总结

标签:rw,Sep,--,rpm,09,正则,剑客,root
From: https://www.cnblogs.com/daofaziran/p/18405162

相关文章

  • 正则表达式
    正则表达式是一种文本模式匹配。它是一个三方产品。常见的shell中有grepsedawk命令支持。通过提供特殊字符来生成一个匹配对应字符串的公式,用此来从海量数据中匹配出想要的数据。1.特殊字符^锚定开头^a以a开头,默认锚定一个字符¥锚定结尾a$以a结尾,默认锚定一个字符......
  • 深入掌握Go语言中的正则表达式与字符串处理
    Go语言中的正则表达式与模式匹配在编程中,字符串处理是常见的需求之一,而正则表达式则是一个强大的工具,能够帮助我们实现复杂的字符串匹配、提取和替换功能。Go语言内置了对正则表达式的支持,通过regexp包,我们可以轻松实现模式匹配的各种操作。本文将详细介绍正则表达式在Go语......
  • 读倪元璐书法--剑客龙天,时成花女
       “骨清年少眼如冰,凤羽参差五色层”,以杜甫诗意起笔评论读倪元璐书法未尚不可,明代末年的社会形势可以说是江河日下,许多忠臣义士,完成其最后的表演,黄道周、倪元璐等,以中原人固有的品格节操,照亮人间,他们的书法墨迹,又是让后来者窥探其忠心的媒介,书为心画,字中表露出来的迹......
  • 【PL/SQL】PL/SQL中的正则表达式
      在Oracle数据库中,同样是有正则表达式的匹配支持的,主要的函数有REGEXP_LIKE ——与like的直接模糊匹配类似;REGEXP_INSTR ——与INSTR的功能类似;REGEXP_SUBSTR ——与SUBSTR的功能相似;REGEXP_REPLACE ——与REPLACE的功能相似;REGEXP_COUNT ——与count的......
  • Python正则表达式
    常用方法re.compile(pattern):编译正则表达式模式,返回一个模式对象,用于匹配操作。提高匹配效率,适用于多次匹配的情况。match():从字符串开头开始匹配,只有在字符串的开头匹配成功时,才返回匹配对象。常用于验证字符串是否符合某种模式。search():在字符串中搜索第一个匹配的......
  • python中正则模块
    importre#1.findall()匹配字符串中所有符合正则的字符串,并返回一个列表result_list=re.findall(r"\d+","我的手机号是13812345678,我的QQ号是456789123")print(result_list,type(result_list))#['13812345678','456789123']<class'list&#......
  • 20240905_182821 python 快速体验正则表达式 获取web的url
    导入正则模块元字符\d,匹配一个数字.,匹配任意符号+,修饰左边的东西让它可以匹配一次或无穷次search方法结果=re.search(规则,目标字符串)如果匹配成功可以有结果如果匹配不成功结果就是Nonesearch的结果如果匹配成功了就会得到一个对象想要拿到匹配的值可以让这个结......
  • 【正则表达式】非捕获组 (?: ... )的使用方法和必要性
    定义非捕获组,用(?:...)表示。这意味着它将匹配括号内的内容,但是不会将匹配的内容存储到内存中供后续引用。这对于那些我们想要作为一个整体处理,但是又不需要单独捕获其内容的情况非常有用。举例假设我们有一个字符串abc123def,并且我们想要匹配其中的数字,同时忽略其他部分......
  • 利用正则表达式从字符串中提取浮点数
    在Python中,使用正则表达式可以非常方便地从字符串中提取浮点数。Python的re模块提供了正则表达式支持。下面是如何使用正则表达式提取浮点数的示例。1、问题背景在开发过程中,有时候我们需要从字符串中提取浮点数,例如从HTML代码中提取价格信息。但是,浮点数的格式可能多种多样......
  • 【Linux入门】正则表达以及sort、uniq、tr、cut命令
    文章目录正则表达1.正则表达式(RegularExpressions)常用的正则表达式元字符:1.基本元字符2.字符类元字符3.特殊字符类4.边界匹配符5.控制字符和转义字符6.贪婪与非贪婪模式示例补充sort命令基本用法常用选项示例uniq命令基本用法常用选项示例tr命令基本用法常用......