1.命令作用
在目录层次结构中搜索文件(search for files in a directory hierarchy.)
2.命令语法
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]
3.参数详解
OPTION:
- -H,其后指定软链接,搜索指定目录及H指定软链接下的文件,其他未指定的软链接下的文件不搜索(了解参数)
- -L,搜索指定目录(包括该目录下的软链接)下的文件
- -P,不搜索指定目录下软链接下的文件,与-L相反,为默认参数,可不指定(了解参数)
- -D,诊断为何find不按指定方式工作,如:find -D help;find -D tree, exec(多个试用逗号分隔)(了解参数)
- -Olevel,重新调整搜索顺序以优化搜索速度,分为0到3,4个等级,其中0与1相同为默认优化等级,2为中度优化等级,3为最高优化等级(了解参数)
- -type,指定文件类型进行搜索
- f,普通文件
- d,目录文件
- l,符号链接文件(软链接)
- s,套接字文件
- b,块设备文件
- c,字符设备文件
- p,管道文件
- 按时间戳查找(以天为单位<time>)
- -atime n,访问时间
- +n,表示(#+1)天之外被访问过
- -n,表示#天之内被访问过
- n,表示短于(#+1) > x >=#天得时间被访问过
- -ctime n,创建时间
- -mtime n,修改时间
- -atime n,访问时间
- 按时间戳查找(以分钟为单位<min>)
- -amin n,访问时间
- +n
- -n
- n
- -cmin n,创建时间
- -mmin n,修改时间
- -amin n,访问时间
- -empty,搜索空文件,可以是空普通文件或者空目录文件
- -name,按照文件名称搜索,可使用*、?、[] 等通配符;*代表多个任意字符,?代表一个任意字符,[]可使用[a|b] 匹配a或者字符
- -size [+-]size[cwbkMG],按照文件大小查找,支持使用+或者-表示大于或小于指定大小,单位可以是c(字节)、w(字数)、b(块数)、k(KB)、M(MB)、G(GB)
- -perm,按照目录文件的读写执行权限搜索
- -executable,按照文件是否可执行搜索
- -a, -and,多个条件文件搜索时,且操作
- -o, -or,多个条件文件搜索时,或操作
- -not,文件搜索时,非操作
- -ls,搜索出得文件大于详细信息,类似ls -l的输出格式
- -exec,将搜索的结果,给exec后的command执行,语法:find [path] [arguments] -exec command {} \+
- {},占位符,用于保存find搜索的结果
- \,表示对每个搜索到的结果都执行command
- +,表示对每个搜索到的结果(所有结果)拼接成一个参数传递给command
- -ok,和-exec作用相同,但是ok会在执行每个命令前给出提示,让用户确定是否执行
4.常用用例
4.1.在根目录下,按照文件名称搜索
[root@node1 ~]# find / -name test.txt
/root/Desktop/test.txt
/root/test.txt
[root@node1 ~]#
4.2.在当前目录下,按照文件类型和文件名称搜索
[root@node1 ~]# find . -type f -name 'test*' # 注意*号匹配需要添加引号
./Desktop/test1.txt
./Desktop/test.txt
./test1.txt
./test.txt
[root@node1 ~]# find . -type f -name '*txt'
./Desktop/test1.txt
./Desktop/test.txt
./test1.txt
./test.txt
[root@node1 ~]#
4.3.在当前目录下,搜索空文件或者目录
[root@node1 ~]# find . -empty # 搜索空文件或者空目录,是递归搜索,会搜索其下的多层空文件或者目录
./.cache/libgweather
./.config/imsettings
./.config/gconf
./Downloads
./Templates
./Public
./Documents
./Music
./Pictures
./Videos
[root@node1 ~]#
4.4.在当前目录下,按照时间搜索
| +5 | 5 | -5 |
<------+-----+-----+-----+-----+-----+-----+-----+-----+-----+
| | | | | | | | | |
9 8 7 6 5 4 3 2 1 当前
Ⅰ 按什么时间搜索文件,选项如下:
atime:文件上次访问的时间(access time)
mtime:文件上次修改的时间(modify time);修改文件内容变更(文件属性变动,mtime不变)
ctime:按照文件状态修改时间搜索(change time);修改文件内容(文属性状态变化,ctime也会变更)
提示:也有-amin、-mmin、-cmin等时间选项,时间单位为分钟
Ⅱ 以上后接数值含义:
-5:-1代表修改时间在1天(24小时)内,-5则代表5天内的文件;
5:1代表1天前2天内(24小时前48小时内)的文件;5则代表5天前6天内的文件;
+5:+0代表1天(24小时)前,+5则代表6天前的文件;
Ⅲ 举例
[root@node1 ~]# find . -mtime +0 -mtime -2 -ls # 查找当前目录下一天以前(+0),两天以内(-2)的文件;即修改时间在24小时前,48小时内的文件
67415571 4 -rw-r--r-- 1 root root 40 Jun 6 00:59 ./test1.txt
67415577 4 -rw-r--r-- 1 root root 215 Jun 6 01:18 ./test.txt
67415587 4 -rw-r--r-- 1 root root 122 Jun 6 03:30 ./perf.log
[root@node1 ~]#
4.5.在当前目录下,按照所有者/所属组搜索
[root@node1 ~]# chown user1:user1 test[1-2].txt
[root@node1 ~]# chown user2:user2 test[3-4].txt
[root@node1 ~]#
[root@node1 ~]# find . -user user1
./test1.txt
./test2.txt
[root@node1 ~]# find . -group user2
./test4.txt
./test3.txt
[root@node1 ~]#
4.6.逻辑运算查找
Ⅰ 逻辑运算说明:
-a: and逻辑与;优先级最高
-o: or逻辑或;优先级次之
-not: not 逻辑非(!);优先级最低
Ⅱ 逻辑运算的例子
[root@node1 ~]# find . -size +0k -a -type f # 所有大于0k的文件(注意: -size +0k 大于0k但不等于0k)
./.bash_logout
./.bash_profile
./.bashrc
... ...
[root@node1 ~]#
[root@node1 ~]# find . -size +0k -a -type f -o -type d # 所有大于0k的目录和文件((不包括不等于0k的目录和文件)),多个type条件需要-o参数或连接
.
./.bash_logout
./.bash_profile
... ...
./.cache
... ...
[root@node1 ~]#
[root@node1 ~]# find . -not -name '*.txt' -size +0k -a -type f -o -type d # 名称非'*.txt',大于0k的文件和目录
.
./.bash_logout
./.bash_profile
... ...
./.cache
... ...
[root@node1 ~]#
Ⅲ 逻辑运算执行顺序
find . cond1 -a cond2 -not cond3 -a cond8 -o cond4 -a cond5 -o cond6 -a cond7
运算执行顺序用括号表示:
find . [cond1 -a cond2 (-not cond3) -a cond8 ] -o (cond4 -a cond5) -o(cond6 -a cond7)
4.7.在当前目录下,按照读写权限搜索
[root@node1 ~]# find . -type d -perm 755 # 搜索当前目录下读写执行权限为755的目录
./.cache/evolution/addressbook
./.cache/evolution/addressbook/trash
./.cache/evolution/calendar
... ...
[root@node1 ~]#
4.8.在当前目录下,按照可执行文件搜索
[root@node1 test]# ll
total 120
-rwxr-xr-x. 1 root root 117680 Jun 7 05:35 ls
-rw-r--r--. 1 root root 215 Jun 7 05:35 test.txt
[root@node1 test]# find . -executable
.
./ls
[root@node1 test]#
4.9.find结果处理(find自带-exec方式)
Ⅰ 命令语法
find [path] [arguments] -exec [command] {} \;
find [path] [arguments] -exec [command] {} \+
Ⅱ 命令语法解释
[command]是您要对 find 命令给出的结果执行的内容。
{} 占位符,用于保存find搜索的结果
\ 表示对每个搜索到的结果都执行command
+ 表示对每个搜索到的结果(所有结果)拼接成一个参数传递给command
-ok和-exec的作用相同。但是ok会在执行每一个命令之前,都会给出提示,让用户来确定是否执行
Ⅱ 举例
[root@node1 test]# find . -name '*.txt*' -exec echo grep -i 1234 {} \;
grep -i 1234 ./test.txt
grep -i 1234 ./test1.txt
grep -i 1234 ./test2.txt
[root@node1 test]# find . -name '*.txt*' -exec echo grep -i 1234 {} \+
grep -i 1234 ./test.txt ./test1.txt ./test2.txt
[root@node1 test]#
4.10.find结果处理(xargs方式)
Ⅰ 从标准输入构建和执行命令行(build and execute command lines from standard input)
Ⅱ find命令结果通过-exec处理时,find命令将结果一起传递给-exec执行。可能会存在长度限制,出现运行几分钟之后出现溢出错误。错误信息通常是"参数列太长"或"参数列溢出"。find与xargs命令一起使用可解决该问题
Ⅲ find命令结果传递给xargs命令处理时,xargs命令是每次只获取一部分文件而不是全部。这样可以先处理最先获取的一部分文件,然后是下一批,并如此继续下去直到结束
Ⅳ 举例
[root@node1 test]# find . -name "*.txt" |xargs -I {} echo grep -i 7777777 {}
grep -i 7777777 ./test.txt
grep -i 7777777 ./test1.txt
grep -i 7777777 ./test2.txt
[root@node1 test]#
[root@node1 test]# find . -name "*.txt" |xargs -I {} grep -i 7777777 {}
[root@node1 test]#