如何发现“修剪"行为? [英] How can this `find -prune`'s behavior be understood?

查看:76
本文介绍了如何发现“修剪"行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据GNU find的联机帮助页,默认情况下使用-print操作,并且

According to the manpage of GNU find, -print action is used by default and

禁止默认-print的动作是-delete-exec-execdir-ok-okdir-fls-fprint-fprintf-ls-printf.

Actions which inhibit the default -print are -delete, -exec, -execdir, -ok, -okdir, -fls, -fprint, -fprintf, -ls, -print and -printf.

所以-prune动作仍应暗示-print动作.

So -prune action should still imply -print action.

实际上是.

$ tree .
.
├── dir/
│   └── file2
└── file1

$ find . -name dir #0
./dir

$ find . -name dir -prune #1
./dir #printed as expected

$ find . -name dir -prune -or -name file1 #2
./file1
./dir #printed as expected

但是,有时-prune禁止使用默认的-print.

However, sometimes -prune inhibits the default -print.

$ find . -name dir -prune -or -name file1 -print #3 #last -print is only added to the above example
./file1

$ find . -name dir -prune -or -print #4
.
./file1

我如何理解这个矛盾?

我的理解:

#1

  1. file1不能满足-name dir的要求,因此被跳过.

  1. file1 doesn't satify -name dir so skipped.

dir满足-name dir的要求,因此将其修剪并将dir添加到TODO列表中.

dir satisfies -name dir so pruned and dir is added to TODO list.

-print还将应用于TODO列表中的dir.

-print is additionally applied to dir in TODO list.

#2

  1. file1满足-name file1的要求,因此已添加到TODO列表中.

  1. file1 satisfies -name file1 so added to TODO list.

#1 -2

-print还将应用于TODO列表中的dirfile1.

-print is additionally applied to dir and file1 in TODO list.

#3

  1. #2 -1

#2 -2

-print应用于TODO列表中的file1.

-print is applied to file1 in TODO list.

-print 应该应另外应用于dir,因为-prune不会禁止-print. (但这是不正确的.为什么?)

-print should additionally be applied to dir because -prune doesn't inhibit -print. (But this is incorrect. WHY?)

#4

  1. file1已添加到TODO列表.

  1. file1 is added to TODO list.

#3 -2

#3 -3

#3 -4

(实际上,find中没有TODO列表.请参见标准.)

(Actually there is no TODO list in find. See this comment and the standard.)

补充:

正如 oguz ismail的答案(现已删除)中指出的那样,我的问题与-prune无关.但是,问题尚未解决.

As pointed out in oguz ismail's answer (deleted now), my question is not related to -prune. However, the question is not solved.

让我们考虑一下-name A -o -name B -print.这分为两个表达式:-name A-name B -print.

Let us think about -name A -o -name B -print. This is broken into two expressions: -name A or -name B -print.

我的理解:第一个表达式-name A没有动作.因此,应隐含-print.换句话说,-name A -o -name B -print应该解释为-name A -print -o -name B -print.

My understanding: The first expression -name A doesn't have an action. So -print should be implied. In other words, -name A -o -name B -print should be interpreted as -name A -print -o -name B -print.

实际行为:-name A -o -name B -print是一种复合表达.此复合表达式中有-print.因此,不应暗示任何其他-print.

Actual behavior: -name A -o -name B -print is one compound expression. There is -print in this compound expression. So no additional -print should be implied.

含糊不清,但我相信我的解释会更自然,因为在这种情况下,每个文件仅满足-name A-name B -print(两个表达式从不同时满足)

There is ambiguity but I believe my interpretation is more natural because, in this case, only -name A or -name B -print is satisfied by each file (both expressions are never satisfied at the same time)

推荐答案

此评论comment 是我的问题,该问题在OP的 Supplement 部分中进行了概述,该问题来自于GNU find的联机帮助页含糊不清,而POSIX有更好的解释.我发现这是真的.

As written in this comment and this comment, my question, which is summarized in the Supplement section in OP, has come from the ambiguity in manpage of GNU find and POSIX has a better explanation. I found this is true.

POSIX

(如果不存在表达式,则将-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:

( given_expression ) -print

并且很自然地解释given_expression是由一个或多个子表达式组成的复合表达式,因为它在括号中是封闭的. (如果此given_expression引用单个子表达式,则括号肯定是多余的.)

and it is natural to interpret given_expression is a compound expression which consists of one or more sub-expressions because it is closed in parenthesis. (If this given_expression referred to a single sub-expression, the parenthesis would definitely be redundant.)

这篇关于如何发现“修剪"行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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