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 特殊符号之引号系列
四剑客命令单独记忆.