我用更简单的解释,手册页是错误的。它应该说
如果整个表达式除 -prune 或 -print 之外不包含任何操作,则对整个表达式为 true 的所有文件执行 -print。
它还应该包含一个警告-quit
,这是一个动作,但它会导致-find
立即退出。所以即使隐含的-print
为整个表达式添加它从未实际执行。
The posix 查找手册页 http://pubs.opengroup.org/onlinepubs/9699919799/utilities/find.html包含更清晰的解释,尽管它没有扩展后那么多的操作gnu
版本。
如果不存在表达式,则应使用 -print 作为表达式。否则,如果给定表达式不包含任何主元 -exec、-ok 或 -print,则给定表达式应有效地替换为:
(给定的表达式)-打印
出于什么gnu
调用动作,posix仅定义-exec
, -ok
, -print
, and -prune
。它没有任何扩展操作-delete
, -ls
等等...所以定义与更正后的一致gnu
一个仅省略-prune
.
以下是一些使用所有 gnu 的示例find
行动证明了这一点。对于所有人来说,请考虑以下文件结构
$ tree
.
└── file
-delete
$ find -name file -delete
$
-执行命令;
$ find -name file -exec echo '-exec is an action so an implicit -print is not applied' \;
-exec is an action so an implicit -print is not applied
$
-execdir命令{} +
$ find -name file -exec echo 'This should print the filename twice if an implicit -print is applied: ' {} +
This should print the filename twice if an implicit -print is applied: ./file
$
-fls
$ find -name file -fls file
$
-fprint
$ find -name file -fprint file
$
-ls
$ find -name file -ls
1127767338 0 -rw-rw-r-- 1 user user 0 May 6 07:15 ./file
$
-ok命令;
$ find -name file -ok echo '-ok is an action so an implicit -print is not applied' \;
< echo ... ./file > ? y
-ok is an action so an implicit -print is not applied
$
-okdir命令;
$ find -name file -okdir echo '-okdir is an action so an implicit -print is not applied' \;
< echo ... ./file > ? y
-okdir is an action so an implicit -print is not applied
$
-print
#./file would be printed twice if an implicit `-print was applied`
$ find -name file -print
./file
$
-print0
#./file would be printed twice if an implicit `-print was applied`
$ find -name file -print0
./file$
-printf
$ find -name file -printf 'Since -printf is an action the implicit -print is not applied\n'
Since -printf is an action the implicit -print is not applied
$
-prune
$ find -name file -prune
./file
$
-quit
$ find -name file -quit
$ find -D opt -name file -quit
...
Optimized command line:
( -name file [0.1] -a [0.1] -quit [1] ) -a [0.1] -print [1]