GNU查找:默认操作何时适用? [英] GNU find: when does the default action apply?

查看:102
本文介绍了GNU查找:默认操作何时适用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Debian 8的find命令的手册页显示:

The man page of Debian 8's find command says:

如果整个表达式除-prune或-print之外均不包含其他操作, -print将对所有表达式为真的所有文件执行.

If the whole expression contains no actions other than -prune or -print, -print is performed on all files for which the whole expression is true.

那么这些输出为何不同:

So why do these outputs differ:

$ 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

因此测试1:表达式是否包含除-prune或-print之外没有其他动作?"好吧,不包括修剪,是的,该陈述是正确的,没有任何动作.因此,这些结果是预期的,因为对于./foo,在-o选项之前的表达式返回True,因此将其打印出来.

So test 1: does the expression contain "no actions other than -prune or -print?" Well, excluding the prune, yes that statement is true, there are no actions. So these results are expected since for ./foo the expression before the -o option returns True, so it's printed.

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

But test 2: does the expression contain "no actions other than -prune or -print?" Well, excluding the prune and the print, yes that statement is true again, there are no other actions. So I would expect the same results.

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

But I don't get ./foo. Why?

手册页应该显示为:如果整个表达式除-prune 或-print 以外不包含任何动作,则-print在所有表达式为真的所有文件上执行. "

It's as if the man page should read: "If the whole expression contains no actions other than -prune or -print, -print is performed on all files for which the whole expression is true."

推荐答案

我要讲的比较简单,手册页是错误的.它应该说

I'm going with the simpler explanation, the man page is wrong. It should instead say

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

If the whole expression contains no actions other than -prune or -print, -print is performed on all files for which the whole expression is true.

它可能还应包含对-quit的警告,这是一个动作,但会导致-find立即退出.因此,即使为整个表达式添加了隐式-print,它也不会真正执行.

It should also maybe contain a caveat for -quit, which is an action, but it causes -find to exit immediately. So even though an implicit -print is added for the whole expression it is never actually executed.

posix查找手册页包含更清晰的解释,尽管它没有比扩展的gnu版本更多的动作.

The posix find man page contains a clearer explanation, though it doesn't have quite as many actions as the expanded gnu version.

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

If no expression is present, -print shall be used as the expression. Otherwise, if the given expression does not contain any of the primaries -exec, -ok, or -print, the given expression shall be effectively replaced by:

(named_expression)-打印

( given_expression ) -print

gnu调用的动作中,posix仅定义了-exec-ok-print-prune.它没有任何扩展的动作-delete-ls等...,因此该定义仅通过省略-prune即可与更正后的gnu匹配.

Out of what gnu calls actions, posix only defines -exec, -ok, -print, and -prune. It does not have any of the expanded actions -delete, -ls, etc... So the definition matches the corrected gnu one by only omitting -prune.

以下是使用所有gnu find动作的一些例子,证明了这一点.对于所有人,请考虑以下文件结构

Here are some examples using all the gnu find actions which prove the point. For all consider the following file structure

$ tree
.
└── file

-删除

$ find -name file -delete
$

-exec命令;

$ 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
$

-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
$

修剪

$ 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]

这篇关于GNU查找:默认操作何时适用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆