英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论