`Find -prune`参数顺序会影响输出列表。

huangapple go评论61阅读模式
英文:

Find -prune argument order affects output list

问题

以下是您要翻译的部分:

我有以下的目录结构(最简单的例子):

    testdir
    testdir/test2
    testdir/test2/test2a
    testdir/test2/test2a/test2aa
    testdir/test1
    testdir/test1/test1a
    testdir/test1/test1a/test1aa
    testdir/test1/test1b
    testdir/test1/test1b/test1ba

我想要找到位于直接位于“testdir”下一层的只有文件夹,不包括特定的文件夹名称,这里是“test1”。首先,输出不排除“test1”的情况:

    user@xyz:/home/user $ find testdir/* -type d -prune
    testdir/test1
    testdir/test2

现在,我可以排除“test1”:

    user@xyz:/home/user $ find testdir/* -type d -prune ! -name test1
    testdir/test2

但是,通过改变“-prune”参数的顺序,会返回“test1”的子目录:

    user@xyz:/home/user $ find testdir/* -type d ! -name test1 -prune
    testdir/test1/test1a
    testdir/test1/test1b
    testdir/test2

为什么最后一个命令会返回子目录“test1a”和“test1b”?我以为选项的顺序不重要,但显然是重要的。据我理解(不确定),“-prune”阻止“find”返回根据设置的“find”选项匹配的模式下方的任何路径。但在“testdir”下面的第一层中,唯一匹配的应该是“test2”。有人能为我澄清这一点吗?
英文:

I have the following directory structure (minimal example):

testdir
testdir/test2
testdir/test2/test2a
testdir/test2/test2a/test2aa
testdir/test1
testdir/test1/test1a
testdir/test1/test1a/test1aa
testdir/test1/test1b
testdir/test1/test1b/test1ba

I would like to find only folders in the depth layer directly below testdir, excluding a certain folder name, here test1. First, the output without excluding test1:

user@xyz:/home/user $ find testdir/* -type d -prune
testdir/test1
testdir/test2

Now, I can exclude test1:

user@xyz:/home/user $ find testdir/* -type d -prune ! -name test1
testdir/test2

However, by changing the order of the -prune argument, subdirectories of test1 are returned:

user@xyz:/home/user $ find testdir/* -type d ! -name test1 -prune
testdir/test1/test1a
testdir/test1/test1b
testdir/test2

Why does the last command return the subdirectories test1a and test1b? I thought the order of the options would not matter, but apparently it does. As far as I understand (not sure about it), -prune prevents find from returning any paths below the patterns matched according to the find options set. But the only match in the first layer below testdir should be test2. Could somebody clarify this for me?

答案1

得分: 4

> Find -prune参数的顺序影响输出列表

这在表面上并不特别令人惊讶。find的行为通常对谓词和操作的相对顺序敏感。

对于find的行为,POSIX规范并不太具体,但我认为GNU find在相关方面的行为在很大程度上都是典型的,即使你实际使用的不是GNU find它的手册页面说:

> GNU find通过从左到右评估给定的表达式来搜索以每个给定的起始点为根的目录树
> [...] 直到结果已知(对于操作,左侧为false,对于操作,左侧为true),此时find会移动到下一个文件名。

(重点添加)

因此,如果你将-name谓词放在-prune操作之前,那么-prune仅对通过-name测试的路径执行,但如果你将-name谓词放在-prune之后,则-name只影响路径是否被打印,而不影响它们是否被修剪。

英文:

> Find -prune argument order affects output list

That's not especially surprising just on the face of it. The behavior of find is sensitive in general to the relative order of predicates and actions.

The POSIX specifications for find's behavior are a bit unspecific, but I think GNU find's behavior is pretty typical in the relevant regards, even if that's not actually the find you are using. Its manual page says:

> GNU find searches the directory tree rooted at each given
> starting-point by evaluating the given expression from left to right
> [...] until
> the outcome is known
(the left hand side is false for and operations,
> true for or), at which point find moves on to the next file name.

(emphasis added).

Thus, if you put the -name predicate before the -prune action then the -prune is performed only for paths that pass the -name test, but if you put the -name predicate after the -prune then the -name affects only whether paths are printed, not whether they are pruned.

huangapple
  • 本文由 发表于 2023年7月31日 20:27:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76803628.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定