1.命令格式
find [查找目录] [查找规则] [查找完后的操作]
即:find pathname -option [-print -exec -ok …]
2.命令功能
用于在文件数中查找文件,并做相应的处理,(有可能访问磁盘)。
3.命令参数
(1)pathname:表示所要查找的目录路径,例如“.”表示当前目录,“/”表示根目录。
(2)-print:将find找到的文件输出到标准输出。
(3)-exec:对找到的文件执行exec这个参数所指定的shell命令,相应的形式为:-exec command {} \;将查到的文件进行command操作,“{}”就代替查到的文件。
注意:
1):“{}”和“\”之间有一个空格。
2):-ok:和-exec的作用相同,只不过-ok更加安全一点,在执行每一个命令之前,系统会让用户确定是否执行。
4.查找规则
(1)根据文件名查找
-name:根据文件名进行查找,区分大小写精确查找。
示例:
[root@localhost etc]# find ./ -name passwd
./passwd
./pam.d/passwd
-iname:根据文件名查找,不区分大小写。
示例:
[root@localhost etc]# find ./ -iname passwd
./Passwd
./catalogue8/passwd
./passwd
“*”:通配任意的字符,可以是任何东西。
示例:
[root@localhost ~]# find ./ -name "file*"
./file1.txt
./file2.txt
“?”:可表示任意单个字符
示例:
[root@localhost ~]# find ./ -name "?.txt"
./1.txt
”[]”:表示通配括号里面的任意一个字符,注意[]里面的内容会被解析成单个字符。
[root@localhost ~]# find ./ -name "file[123].txt"
./file1.txt
./file2.txt
(2)根据文件的时间戳信息查找文件
在根据时间戳信息查找的时候,所有的time都是以天为单位,min都是以分钟为单位。+n表示n以前,-n表示n以内。
1):以最近一次存取的时间为参数
-atime:
A、find pathname -atime +n //表示n天前存取过的文件
B、find pathname -atime -n //表示以当前时间为起点前n天内存取过的文件
示例:
[root@localhost ~]# find ./ -atime +1
./.bash_logout
./.cshrc
./.tcshrc
./anaconda-ks.cfg
[root@localhost ~]# find ./ -atime -1
./
./.bashrc
./.bash_history
./.bash_profile
./hsz
-amin :
A、find pathname -amin +n //表示n分钟前存取过的文件。
B、find pathname -amin -n //表示以当前时间为起点前n分钟内存取过的文件。
示例:
[root@localhost ~]# find ./ -amin +1
./
./.bash_logout
./.bashrc
./.cshrc
./.tcshrc
[root@localhost ~]# find ./ -amin -1
./
2):以最近一次修改的时间为参数
-mtime:
A、find pathname -mtime +n //表示n天前修改过的文件
B、find pathname -mtime -n //表示以当前时间为起点前n天内修改 过的文件
示例:
[root@localhost ~]# find ./ -mtime +1
./.bash_logout
./.bashrc
./.cshrc
./.tcshrc
[root@localhost ~]# find ./ -mtime -1
./
./test
./test/A
./f1.txt
./jiqi
./bawei.txt
-mmin:
A、find pathname -mmin +n //表示n分钟前存取过的文件
B、find pathname -mmin -n //表示以当前时间为起点前n修改内存取过的文件
示例:
[root@localhost ~]# find ./ -mmin +1
./
./.bash_logout
./.bashrc
./.cshrc
[root@localhost ~]# find ./ -mmin -1
[root@localhost ~]#
3)以最近一次更改的属性为参数
-ctime:
A、find pathname -ctime +n //表示n天前更改过的文件
B、find pathname -ctime -n //表示以当前时间为起点前n天内更改过的文件
示例:
[root@localhost ~]# find ./ -ctime +1
./.bash_logout
./.bashrc
./.cshrc
./.tcshrc
./anaconda-ks.cfg
./.bash_history
./.bash_profile
[root@localhost ~]# find ./ -ctime -1
./
./test
./test/A
./myfile.txt
./f1.txt
./jiqi
./bawei.txt
-cmin:
A、find pathname -cmin +n //表示n分钟前更改过的文件
B、find pathname -cmin -n //表示以当前时间为起点前n分钟内更改过的文件
示例:
[root@localhost ~]# find ./ -cmin +2
./
./.bash_logout
./.bashrc
./.cshrc
[root@localhost ~]# ll myfile.txt
-rw-r--r--. 1 root root 0 7月 18 19:46 myfile.txt
[root@localhost ~]# chgrp root myfile.txt
[root@localhost ~]# ll myfile.txt
-rw-r--r--. 1 root root 0 7月 18 19:46 myfile.txt
[root@localhost ~]# find ./ -cmin -2
./myfile.txt
(3)根据文件所属用户和所属组来查找文件
1)根据文件所有者查找文件:-user
2)根据所有者所在的组查找:-group
示例:
[root@localhost ~]# find ./ -user root
./
./.bash_logout
./.bashrc
./.cshrc
./.tcshrc
./anaconda-ks.cfg
./.bash_history
./.bash_profile
./hsz
./test
[root@localhost ~]# find ./ -group root
./
./.bash_logout
./.bashrc
./.cshrc
./.tcshrc
./anaconda-ks.cfg
./.bash_history
(4)根据nouser或nogroup查找
1)查找无有效属主的文件:-nouser
find pathname -nouser
2)查找无有效属组的文件:-nogroup
find pathname -nogroup
(5)-perm:根据权限来查找文件
示例:
[root@localhost ~]# find ./ -perm 644
./.bash_logout
./.bashrc
./.cshrc
./.tcshrc
./.bash_profile
./test/A
./myfile.txt
./txt.a
./f2.txt
(6)根据uid和gid查找文件
1)根据文件的uid查找:-uid
2)根据文件所在组的gid查找:-gid
(7)-type:根据文件类型查找文件
1)普通文件:f
find pathname -type f
2)目录文件:d
find pathname -type d
3)链接文件:l
find pathname -type l
4)块设备文件:b
find pathname -type b
5)字符设备文件:c
find pathname -type c
6)管道设备文件:p
find pathname -type p
7)套接字文件:s
find pathname -type s
示例:
[root@localhost ~]# find ./ -type f
./.bash_logout
./.bashrc
./.cshrc
./.tcshrc
./anaconda-ks.cfg
./.bash_history
./.bash_profile
[root@localhost ~]# find ./ -type d
./
./hsz
./test
./test/a
./test/b
./test/c
(8)根据文件大小查找文件:-size
1)find pathname -size +n //表示大于n字节的文件
2)find pathname -size -n //表示小于n字节的文件
3)find pathname -size n //表示等于n字节的文件
示例:
[root@localhost ~]# find ./ -size 2
./anaconda-ks.cfg
[root@localhost ~]# find ./ -size +2
./
./.bash_history
./.test.swp
./.swp
./.viminfo
[root@localhost ~]# find ./ -size -2
./.bash_logout
./.bashrc
./.cshrc
./.tcshrc
./.bash_profile
./hsz
(9)按照参考文件的更改时间查找
-newer:file1 //查找更改时间比file1的更改时间距离当前时间近的文件
示例:
[root@localhost ~]# touch file3
[root@localhost ~]# touch file4
[root@localhost ~]# find ./ -newer file3
./
./file4
(10)maxdepth和mindepth
-
-maxdepth n:搜索深度距离当前目录最多n个子目录深度
-
-mindepth n:搜索深度距离当前目录至少n个子目录深度
示例:[root@localhost ~]# find ./ -maxdepth 1 -name file3 ./file3 [root@localhost ~]# find ./ -maxdepth 2 -name file3 ./file3 [root@localhost ~]# find ./ -name file3 ./file3 [root@localhost ~]# find . -mindepth 1 -name file3 ./file3 [root@localhost ~]# find . -mindepth 2 -name file3 [root@localhost ~]# find ./ -name file3 ./file3
(11)查找路径下为空的文件或文件夹:-empty
示例:
[root@localhost ~]# find ./ -empty
./hsz
./test/a
./test/b
./test/c
./test/A
./a_c
./a_d
./b_c
[root@localhost ~]# ll
总用量 28
-rwxr-xr-x. 1 root root 0 7月 18 19:57 1.txt
-rwxr-xr-x. 1 root root 0 7月 19 20:13 abc.txt
drwxr-xr-x. 2 root root 6 7月 17 20:01 a_c
drwxr-xr-x. 2 root root 6 7月 17 20:01 a_d
(12)按照硬连接的数目进行查找
1)-link n:硬连接连接数等于n的文件或目录
2)-link +n:硬连接连接数大于n的文件或目录
3)-link -n:硬连接连接数小于n的文件或目录
示例:
[root@localhost ~]# find -links 1
./.bash_logout
./.bashrc
./.cshrc
./.tcshrc
./anaconda-ks.cfg
./.bash_history
./.bash_profile
[root@localhost ~]# find -links 2
./hsz
./test/a
./test/b
./test/c
./a_c
./a_d
./b_c
./b_d
[root@localhost ~]# find -links +2
.
./test
[root@localhost ~]# find -links +1
.
./hsz
./test
./test/a
./test/b
./test/c
[root@localhost ~]# find -links -2
./.bash_logout
./.bashrc
./.cshrc
./.tcshrc
(13)-a、-o、-not(也就是与、或、非)
1)-a:连接两个不同的条件(即两个条件必须同时为真)
2)-o:连接两个不同的条件(两个条件只要满足一个即可)
3)-not:对条件进行取反
示例:
[root@localhost ~]# find ./ -size +0 -a -size -9
./
./.bash_logout
./.bashrc
./.cshrc
./.tcshrc
./anaconda-ks.cfg
./.bash_profile
[root@localhost ~]# find ./ -size 8 -o -name file
./
[root@localhost ~]# find ./ -not -size 8
./.bash_logout
./.bashrc
./.cshrc
./.tcshrc
(14)查找完成之后的操作
1) -exec command {} ;: 其中,”{}”就代表查找之后返回的文件。
2)-ok command {} ;-ok相对于-exec而言更安全一点,会让用户确定所要执行的操作。
示例:
[root@localhost ~]# find ./ -name file3 -exec ls -l {} \;
-rw-r--r--. 1 root root 0 7月 22 18:26 ./file3
[root@localhost ~]# find ./ -name file3 -ok ls -l {} \;
< ls ... ./file3 > ? y
-rw-r--r--. 1 root root 0 7月 22 18:26 ./file3