此例中+100c表示当前目录下大于100 bytes的文件,n和前面表示时间的方式类似(+n,-n,n),n后面的字符还包括:
b 单位为512 bytes的块(n后面没有后缀时的默认单位)
k 1024 bytes
M 1048576 bytes
G 1073741824 bytes
选项-print0类似-print输出文件名,但不用任何字符分隔它们。当文件名中包含特殊字符时使用。可以配合带选项-0的命令xargs一起使用(后述)。
选项-exec command ;表示要执行的命令
-exec后可以跟任意shell命令来对搜索到的文件做进一步的处理,在command和分号之间都被视为command的参数,其中用{}代表被搜索到的文件。分号需要被转义。
如对搜索到的文件执行命令ls -l:
[root@centos7 temp]# find . -name "file*" -exec ls -l {} ;
-rw-r--r-- 1 root root 132 10月 27 13:28 ./file10
-rw-r--r-- 1 root root 22 10月 26 21:31 ./file12
-rw-r--r-- 1 root root 64 10月 27 15:06 ./file11
-rw-r--r-- 1 root root 67 10月 31 17:50 ./tmp/file
-rw-r--r-- 1 root root 0 11月 1 12:05 ./abcd/file15
-rwxrwxrwx 1 root root 137 10月 12 16:42 ./file13
lrwxrwxrwx 1 root root 6 11月 1 12:29 ./file14 -> file13
-exec选项后的命令是在启动find所在的目录内执行的,并且对于每个搜索到的文件,该命令都执行一次,而不是把所有文件列在命令后面只执行一次。
举例说明下其中的区别:
#命令echo只执行一次
[root@centos7 temp]# echo ./file11 ./file12 ./file13
./file11 ./file12 ./file13
#命令echo执行了三次
[root@centos7 temp]# find . -name "file1[1-3]" -exec echo {} ;
./file12
./file11
./file13
[root@centos7 temp]#
当使用格式-exec command {} +时表示每个文件都被追加到命令后面,这样,命令就只被执行一次了:
[root@centos7 temp]# find . -name "file1[1-3]" -exec echo {} +
./file12 ./file11 ./file13
[root@centos7 temp]#
但有时会出现问题:
[root@centos7 temp]# find . -name "file1[1-3]" -exec mv {} abcd/ +
find: 遗漏“-exec”的参数
[root@centos7 temp]#
因为这里文件被追加于目录abcd/的后面,导致报错。
同时,使用格式-exec command {} +还可能会造成被追加的文件数过多,超出了操作系统对命令行长度的限制。
使用-exec可能会有安全漏洞,通常使用管道和另一个命令xargs来代替-exec执行命令。
2、xargs 从标准输入中获得命令的参数并执行
xargs从标准输入中获得由空格分隔的项目,并执行命令(默认为/bin/echo)
选项-0将忽略项目的分隔符,配合find的选项-print0,处理带特殊符号的文件。
[root@centos7 temp]# find . -name "file*" -print0 | xargs -0 ls -l -rw-r--r-- 1 root root 132 10月 27 13:28 ./file10 -rw-r--r-- 1 root root 64 10月 27 15:06 ./file11 -rw-r--r-- 1 root root 22 10月 26 21:31 ./file12 -rwxrwxrwx 1 root root 137 10月 12 16:42 ./file13 -rw-r--r-- 1 root root 0 11月 1 14:45 ./file 14 #注意此文件名中包含空格










