加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

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

发布时间:2020-12-14 01:02:34 所属栏目:Linux 来源:网络整理
导读:Debian 8的find命令的手册页说: 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. 那么为什么这些产出不同: $mkdir -p test/foo test/bar cd test t
Debian 8的find命令的手册页说:

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.

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

$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,因此它被打印出来.

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

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

就好像手册页应该是这样的:“如果整个表达式不包含-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.

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

posix find man page包含更清晰的解释,但它没有扩展的gnu版本那么多的动作.

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:

( given_expression ) -print

在gnu调用的操作中,posix仅定义-exec,-print和-prune.它没有任何扩展的动作-delete,-ls等……所以定义只通过省略-prune来匹配纠正的gnu.

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读