find
find命令相对于locate这种非实时查找的搜索命令,大大增加了我们搜索的便捷度以及准确性;并且能够方便的帮助我们对大文件、特定类型的文件查找与删除,特别是有超多小碎文件的时候,更是方便至极…
注意:find命令在查找某个目录下的文件时,查找的条件是包括该目录本身的
此处介绍find命令基本用法,find命令的所有用法可以用man命令详细查找
find -name ##按名字查找
-maxdepth ##按照最大深度查找
-mindepth ##按照最小深度查找
-not ##不是的意思,非的意思
-user ##按照用户名查找
-group ##按照组名查找
-a ##and 并且 条件都满足
-o ##or 或者 条件满足其中一个
-size 20k ##按照文件大小查找,查找大小为20k的文件
-size -20k ##查找大小小于20k的文件
-size +20k ##查找大小大于20k的文件
-type ##按照文件类型查找(文件类型分类见part_05)
-ctime 10 ##文件最近一次更改时间距离现在是10天
-ctime +|-10 ##文件最近一次更改时间距离现在超过(+)10天,(-)小于10天
-cmin 10 ##文件最近一次更改时间距离现在是10分钟
-cmin +|-10 ##文件最近一次更改时间距离现在超过(+)10分钟,(-)小于10分钟
-perm /444 ##按照文件权限查找,解释详见下面的例子
-perm 444
-perm -444
-exec command {} \; ##查找出某个文件并执行某个命令
find命令用法举例:
1 [root@squid mnt]# find file1 ##按名字查找,查找/mnt下名字为file1的文件
file1
2 [root@squid mnt]# find /etc -maxdepth 2 -name passwd ##按最大深度查找,查找/etc下,最大深度为2的名字为passwd的文件
/etc/passwd
/etc/pam.d/passwd
3 [root@squid mnt]# find /etc -mindepth 2 -name passwd ##按最浅深度查找,查找/etc下,最小深度为2的名字为passwd的文件
/etc/pam.d/passwd
4 [root@squid mnt]# ll
total 0
-rw-r--r--. 1 root root 0 Mar 14 06:44 file1
-rw-r--r--. 1 student student 0 Mar 14 06:44 file2
-rw-r--r--. 1 student root 0 Mar 14 06:44 file3
-rw-r--r--. 1 student westos 0 Mar 14 06:44 file4
-rw-r--r--. 1 westos westos 0 Mar 14 06:44 file5
-rw-r--r--. 1 westos root 0 Mar 14 06:44 file6
-rw-r--r--. 1 westos student 0 Mar 14 06:44 file7
[root@squid mnt]# find -user student -a -group westos ##查找/mnt下用户为student,和组为group的文件
./file4
5 [root@squid mnt]# find -user student -o -group westos ##查找/mnt下用户为student,或者组为group的文件
./file2
./file3
./file4
./file5
6 [root@squid mnt]# find -user student -a -not -group westos ##查找/mnt下用户为student,和组不是group的文件
./file2
./file3
[root@squid mnt]# dd if=/dev/zero of=/mnt/file3 bs=1024 count=40 ##截取/dev/zero文件以1024为单位的40k大小的文件 到/mnt/file3
40+0 records in
40+0 records out
40960 bytes (41 kB) copied, 0.000149333 s, 274 MB/s
[root@squid mnt]# ll
total 104
-rw-r--r--. 1 root root 10240 Mar 14 07:04 file
-rw-r--r--. 1 root root 20480 Mar 14 07:08 file1
-rw-r--r--. 1 root root 30720 Mar 14 07:08 file2
-rw-r--r--. 1 root root 40960 Mar 14 07:08 file3
7 [root@squid mnt]# find /mnt -size -20k ## #查找/mnt下的大小为小于20k的文件
/mnt
/mnt/file
8 [root@squid mnt]# find /mnt -size +20k ###查找/mnt下的大小为大于20k的文件
/mnt/file2
/mnt/file3
9 [root@squid mnt]# find /mnt -size 20k ###查找/mnt下的大小为20k的文件
/mnt/file1
10 [root@squid mnt]# find /mnt -type d ##查找/mnt下类型为目录的文件
/mnt
11 [root@squid mnt]# find /mnt -ctime -10 ##查找/mnt下最近一次修改时间距离现在小于10天的文件
/mnt
/mnt/file
/mnt/file1
/mnt/file2
/mnt/file3
12 [root@squid mnt]# find /mnt -cmin +10 ##查找/mnt下最近一次修改时间距离现在超过10分钟的文件
/mnt
/mnt/file
/mnt/file1
/mnt/file2
/mnt/file3
[root@squid mnt]# ll
total 104
-rwxrwxrwx. 1 root root 10240 Mar 14 07:04 file
-r--------. 1 root root 20480 Mar 14 07:08 file1
-r--r--r--. 1 root root 30720 Mar 14 07:08 file2
-rw-rw-rw-. 1 root root 40960 Mar 14 07:08 file3
-rw-r--r--. 1 root root 0 Mar 14 07:28 file4
--w--w--w-. 1 root root 0 Mar 14 07:28 file5
13 [root@squid mnt]# find /mnt -perm 444 ###查找/mnt下文件权限为444的文件
/mnt/file2
14 [root@squid mnt]# find /mnt -perm -444查找/mnt下文件权限包括444的文件,就是u g o 三位中必须都包含6的文件 777也是可以查找到的,因为777中包含444
/mnt
/mnt/file
/mnt/file2
/mnt/file3
/mnt/file4
15 [root@squid mnt]# find /mnt -perm /444 ###查找/mnt下文件权限包括一个4的文件就是u g o 三位中包含一个4就行 "/和+是一样的作用,企业七之前的是+ 现在企业七是/"
/mnt
/mnt/file
/mnt/file1
/mnt/file2
/mnt/file3
/mnt/file4
16 [root@squid mnt]# find /mnt -size 20k -exec rm -rf {} \; ##查找/mnt下的大小为20k的文件 ###删除/mnt下大小为20k的文件//
[root@squid mnt]# ll
total 84
-rwxrwxrwx. 1 root root 10240 Mar 14 07:04 file
-r--r--r--. 1 root root 30720 Mar 14 07:08 file2
-rw-rw-rw-. 1 root root 40960 Mar 14 07:08 file3
-rw-r--r--. 1 root root 0 Mar 14 07:28 file4
--w--w--w-. 1 root root 0 Mar 14 07:28 file5