我将使用更简单的解释,手册页是错误的。它应该说
如果整个表达式不包含除 -prune或 -print之外的任何操作,则对整个表达式为真的所有文件执行 -print。
它还应该包含一个警告-quit
,这是一个动作,但它会导致-find
立即退出。因此,即使-print
为整个表达式添加了隐式,它也不会真正执行。
posix find 手册页包含更清晰的解释,尽管它没有扩展gnu
版本那么多的操作。
如果不存在表达式,则应使用 -print 作为表达式。否则,如果给定的表达式不包含任何主要的 -exec、-ok 或 -print,则给定的表达式应有效地替换为:
(给定表达式)-打印
在gnu
所谓的动作中,posix 只定义-exec
、-ok
、-print
和-prune
。它没有任何扩展的动作-delete
,-ls
等等......所以定义匹配更正的gnu
一个只省略-prune
。
以下是一些使用所有 gnufind
操作的示例,可以证明这一点。对于所有考虑以下文件结构
$ tree
.
└── file
-删除
$ 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
$
-打印
#./file would be printed twice if an implicit `-print was applied`
$ find -name file -print
./file
$
-打印0
#./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
$
-修剪
$ find -name file -prune
./file
$
-退出
$ 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]