GNU find:默认操作何时适用?

art*_*bot 5 linux find gnu-findutils

Debian 8的find命令的手册页说:

如果整个表达式不包含-prune或-print以外的任何操作,则对整个表达式为true的所有文件执行-print.

那么为什么这些产出不同:

$ mkdir -p test/foo test/bar && cd test && touch foo/bar bar/foo
$ # Test 1
$ find . -name foo -type d -prune -o -name foo
./foo
./bar/foo
$ # Test 2
$ find . -name foo -type d -prune -o -name foo -print
./bar/foo
Run Code Online (Sandbox Code Playgroud)

所以测试1:表达式是否包含"除-prune或-print之外的其他动作?" 好吧,除了修剪,是的,这句话是真的,没有行动.所以这些结果是预期的,因为对于选项./foo之前的表达式-o返回True,所以它被打印出来.

但测试2:表达式是否包含"除-prune或-print之外的其他操作?" 好吧,不包括修剪和印刷品,是的,该陈述再次成立,没有其他行动.所以我希望得到相同的结果.

但我没有得到./foo.为什么?

就好像手册页应该是:"如果整个表达式不包含-prune以外的任何动作 或打印,-print对整个表达式为真的所有文件执行."

Rei*_*ase 3

我将采用更简单的解释,手册页是错误的。它应该说

\n
\n

如果整个表达式除 -prune或 -print之外不包含任何操作,则对整个表达式为 true 的所有文件执行 -print 。

\n
\n

它还应该包含 的警告-quit,这是一个操作,但它会导致-find立即退出。因此,即使-print为整个表达式添加了隐式,它也永远不会被实际执行。

\n

posix find 手册页包含更清晰的解释,尽管它没有扩展gnu版本那么多的操作。

\n
\n

如果不存在表达式,则应使用 -print 作为表达式。否则,如果给定表达式不包含任何主元 -exec、-ok 或 -print,则给定表达式应有效地替换为:

\n

(给定的表达式)-打印

\n
\n

gnu所谓的动作中,posix 只定义了-exec-ok-print-prune。它没有任何扩展的操作-delete-ls等...因此该定义gnu仅通过省略 来匹配更正的操作-prune

\n

下面是一些使用所有 gnufind操作的示例来证明这一点。对于所有人来说,请考虑以下文件结构

\n
$ tree\n.\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 file\n
Run Code Online (Sandbox Code Playgroud)\n

-删除

\n
$ find -name file -delete\n$\n
Run Code Online (Sandbox Code Playgroud)\n

-执行命令;

\n
$ find -name file -exec echo \'-exec is an action so an implicit -print is not applied\' \\;\n-exec is an action so an implicit -print is not applied\n$\n
Run Code Online (Sandbox Code Playgroud)\n

-execdir 命令 {} +

\n
$ find -name file -exec echo \'This should print the filename twice if an implicit -print is applied: \' {} +\nThis should print the filename twice if an implicit -print is applied:  ./file\n$\n
Run Code Online (Sandbox Code Playgroud)\n

-fls

\n
$ find -name file -fls file\n$\n
Run Code Online (Sandbox Code Playgroud)\n

-f打印

\n
$ find -name file -fprint file\n$\n
Run Code Online (Sandbox Code Playgroud)\n

-ls

\n
$ find -name file -ls\n1127767338    0 -rw-rw-r--   1 user   user          0 May  6 07:15 ./file\n$\n
Run Code Online (Sandbox Code Playgroud)\n

-ok命令;

\n
$ find -name file -ok echo \'-ok is an action so an implicit -print is not applied\' \\;\n< echo ... ./file > ? y\n-ok is an action so an implicit -print is not applied\n$\n
Run Code Online (Sandbox Code Playgroud)\n

-okdir命令;

\n
$ find -name file -okdir echo \'-okdir is an action so an implicit -print is not applied\' \\;\n< echo ... ./file > ? y\n-okdir is an action so an implicit -print is not applied\n$\n
Run Code Online (Sandbox Code Playgroud)\n

-打印

\n
#./file would be printed twice if an implicit `-print was applied`\n$ find -name file -print\n./file\n$\n
Run Code Online (Sandbox Code Playgroud)\n

-打印0

\n
#./file would be printed twice if an implicit `-print was applied`\n$ find -name file -print0\n./file$\n
Run Code Online (Sandbox Code Playgroud)\n

-printf

\n
$ find -name file -printf \'Since -printf is an action the implicit -print is not applied\\n\'\nSince -printf is an action the implicit -print is not applied\n$\n
Run Code Online (Sandbox Code Playgroud)\n

-修剪

\n
$ find -name file -prune\n./file\n$\n
Run Code Online (Sandbox Code Playgroud)\n

-辞职

\n
$ find -name file -quit\n$ find -D opt -name file -quit\n...\nOptimized command line:\n( -name file [0.1] -a [0.1] -quit [1]  ) -a [0.1] -print [1]\n
Run Code Online (Sandbox Code Playgroud)\n