1、基础
1.1、为什么需要查找
1、很多时候我们可能会忘了文件所在的位置,此时就需要通过find来查找;
2、有时候需要通过内容查找到对应的文件,此时就需要通过find来查找;
1.2、为什么是find
因为find命令可以根据不同的条件来进行查找文件·比如:
文件名称
文件大小
文件时间
属主属组
权限等可以通过如上几种方式查找文件,从而实现精准定位。
1.3、语法
命令 路径 选项 表达式 动作
find [path...] [options] [expression] [action]
2、示例1-基于【名称】查找
2.1、创建演示数据
mkdir /etc/sysconfig/network-scripts/{ifcfg-eth1,IFCFG-ETH1}
[root@linux ~]# ll /etc/sysconfig/network-scripts/
total 232
drwxr-xr-x 2 root root 6 Apr 20 21:13 ifcfg-eth1
drwxr-xr-x 2 root root 6 Apr 20 21:13 IFCFG-ETH1
...
2.2、按文件名字查询【默认文件名字母大小有区分】
]# find /etc/ -name 'ifcfg-eth1'
/etc/sysconfig/network-scripts/ifcfg-eth1
2.3、按文件名字查询【忽略文件名大小写字母】
]# find /etc/ -iname 'ifcfg-eth1'
/etc/sysconfig/network-scripts/ifcfg-eth1
/etc/sysconfig/network-scripts/IFCFG-ETH1
2.4、正则匹配查询文件名
find /etc/ -iname 'ifcfg-eth*'
find /etc/ -name 'ifcfg-eth*'
3、示例2-基于【文件大小】查找
3.1、查询大于5M文件
3.2、查询等于5M文件
3.3、查询小于5M文件
4、示例3-基于【文件类型】查找
4.1、查询类型为文件
4.2、查询类型为目录
4.3、查询类型为链接
4.4、查询类型为块设备
4.5、查询类型为字符设备
4.6、查询类型为套接字
4.7、查询类型为管道文件
5、示例4-基于【时间】查找
5.1、基于时间查询的原理图

5.2、创建演示数据
for i in {01..20};do date -s 202304$i && touch /tmp/file-$i;done
[root@linux ~]# ll /tmp/file-*
-rw-r--r-- 1 root root 0 Apr 1 00:00 /tmp/file-01
-rw-r--r-- 1 root root 0 Apr 2 00:00 /tmp/file-02
-rw-r--r-- 1 root root 0 Apr 3 00:00 /tmp/file-03
-rw-r--r-- 1 root root 0 Apr 4 00:00 /tmp/file-04
-rw-r--r-- 1 root root 0 Apr 5 00:00 /tmp/file-05
-rw-r--r-- 1 root root 0 Apr 6 00:00 /tmp/file-06
-rw-r--r-- 1 root root 0 Apr 7 00:00 /tmp/file-07
-rw-r--r-- 1 root root 0 Apr 8 00:00 /tmp/file-08
-rw-r--r-- 1 root root 0 Apr 9 00:00 /tmp/file-09
-rw-r--r-- 1 root root 0 Apr 10 00:00 /tmp/file-10
-rw-r--r-- 1 root root 0 Apr 11 00:00 /tmp/file-11
-rw-r--r-- 1 root root 0 Apr 12 00:00 /tmp/file-12
-rw-r--r-- 1 root root 0 Apr 13 00:00 /tmp/file-13
-rw-r--r-- 1 root root 0 Apr 14 00:00 /tmp/file-14
-rw-r--r-- 1 root root 0 Apr 15 00:00 /tmp/file-15
-rw-r--r-- 1 root root 0 Apr 16 00:00 /tmp/file-16
-rw-r--r-- 1 root root 0 Apr 17 00:00 /tmp/file-17
-rw-r--r-- 1 root root 0 Apr 18 00:00 /tmp/file-18
-rw-r--r-- 1 root root 0 Apr 19 00:00 /tmp/file-19
-rw-r--r-- 1 root root 0 Apr 20 00:00 /tmp/file-20
5.3、查找7天以前的文件(不会打印当天的文件)
# 当前时间:2023-04-20
]# find /tmp/ -iname 'file-*' -mtime +7
/tmp/file-01
/tmp/file-02
/tmp/file-03
/tmp/file-04
/tmp/file-05
/tmp/file-06
/tmp/file-07
/tmp/file-08
/tmp/file-09
/tmp/file-10
/tmp/file-11
/tmp/file-12
# 即是7天以后的数据
5.4、查找最近7天的文件,不建议使用(会打印当天的文件)
# 当前时间:2023-04-20
]# find /tmp/ -iname 'file-*' -mtime -7
/tmp/file-14
/tmp/file-15
/tmp/file-16
/tmp/file-17
/tmp/file-18
/tmp/file-19
/tmp/file-20
5.5、查找第7天文件(不会打印当天的文件)
# 当前时间:2023-04-20
]# find /tmp/ -iname 'file-*' -mtime 7
/tmp/file-13
5.6、保留最近7天内的file-*,其余删除
]# find /tmp/ -iname 'file-*' -mtime +7 -delete
]# find /tmp/ -iname 'file-*'
/tmp/file-13
/tmp/file-14
/tmp/file-15
/tmp/file-16
/tmp/file-17
/tmp/file-18
/tmp/file-19
/tmp/file-20
使用xargs配置删除也可以
find /tmp/ -iname 'file-*' -mtime +7 | xargs rm -f
6、示例5-基于【用户】查找
6.1、查询所属
6.1.1、所属主
6.1.2、所属组
6.1.3、所属主和所属组
find /home/ -group cyc -user cyc
6.2、查询没有所属
6.2.1、没有所属主
6.2.2、没有所属组
6.2.3、没有所属主和所属组
find /home/ -nouser -nogroup
7、示例6-基于【权限】查找
7.1、精准权限查询
]# find /etc -type f -perm 644 -ls | wc -l
341
7.2、包含权限查询
]# find /etc -type f -perm -644 -ls| wc -l
400
7.3、特殊权限查询
find /usr/bin/ /usr/sbin/ -type f -perm -1000 -ls
find /usr/bin/ /usr/sbin/ -type f -perm 2000 -ls
]# find /usr/bin/ /usr/sbin/ -type f -perm -4000 -ls
50453253 76 -rwsr-xr-x 1 root root 73888 Aug 9 2019 /usr/bin/chage
50453254 80 -rwsr-xr-x 1 root root 78408 Aug 9 2019 /usr/bin/gpasswd
...
8、示例7-基于【逻辑运算符】查找
8.1、运算符介绍
8.2、查找当前目录下,属主不是root的所有文件
find . -not -user root
find . ! -user root
8.3、查找当前目录下,属主属于cyc,并且大小大于1k的文件
find . -type f -user cyc -a -size +1k
8.4、查找当前目录下的属主为root或者以xml结尾的普通文件
find . -type f -user root -a -iname '*.xml'
9、示例8-基于【动作处理】
9.1、find动作介绍
动作 含义
-print 打印查找到的内容(默认)
-ls 以长格式显示的方式打印查找到的内容
-delete 删除查找到的文件(仅能删除空目录)
-ok 后面跟自定义shell命令(会提示是否操作)
-exec 后面跟自定义shell命令(标准写法-exec \;)
9.2、find与exec
9.2.1、查询拷贝文件
find /etc/ -iname 'ifcfg*' -exec cp -rfv {} /tmp/ \;
9.2.2、删除文件-示例
find /tmp/ -type f -name 'ifcfg*' -exec rm -f {} \;
9.3、find与xargs
9.3.1、删除文件-示例
find /tmp/ -type f -name 'file-*' | xargs rm -f
9.4、find与grep
9.4.1、按文件内容查询
echo "www.test.com" >/tmp/www.html
]# find /tmp/ -type f | xargs grep -R 'www\.test\.com' 2>/dev/null
/tmp/www.html:www.test.com