Linux find 中的 -prune 选项有什么作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6896564/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does -prune option in find do?
提问by kingsmasher1
I can see the -prune of find
not working correctly. I guess -name "efence*" -prune
option should select (or find) all files except the one with name efence*
right?
我可以看到 -prunefind
无法正常工作。我猜-name "efence*" -prune
选项应该选择(或查找)除名称以外的所有文件,efence*
对吗?
Or am i wrong in my understanding?
还是我的理解有误?
The command i executed:
find * -maxdepth 0 -name "efence*" -prune
我执行的命令:
find * -maxdepth 0 -name "efence*" -prune
Expectation: Select all files at current directory (maxdepth 0
) except one with name *efence.
期望:选择当前目录 ( maxdepth 0
) 中的所有文件,但名为 *efence 的文件除外。
Please help me to understand -prune
请帮我理解 -prune
采纳答案by OpenSauce
Try
尝试
find * -maxdepth 0 -name "efence*" -prune -o -print
The prune
option doesprint matching files, if no other options are specified (it still prevents find
from recursing into matching directories, however).
如果没有指定其他选项,该prune
选项会打印匹配的文件(但是,它仍然可以防止find
递归到匹配的目录中)。
Edited to add explanation:
编辑添加解释:
find
expressions distinguish between tests
and actions
. From man find
:
find
表达式区分tests
和actions
。来自man find
:
The expression is made up of options (which affect overall operation rather than the processing of a specific file, and always return true), tests (which return a true or false value), and actions (which have side effects and return a true or false value), all separated by operators.
-and
is assumed where the operator is omitted.If the expression contains no actions other than
-prune
,
表达式由选项(影响整体操作而不是特定文件的处理,并始终返回 true)、测试(返回 true 或 false 值)和操作(具有副作用并返回 true 或false 值),全部由运算符分隔。
-and
假定省略了运算符。如果表达式不包含除 之外的任何操作
-prune
,
So -prune
is an action which has the side effect that find
will not recurse into subdirectories which match the preceding test (in your case, -maxdepth 0 -name "efence*"
). But in terms of the truth-value of the expression, it's equivalent to just having
-prune
具有副作用的操作也是如此,该操作find
不会递归到与前面的测试匹配的子目录中(在您的情况下为-maxdepth 0 -name "efence*"
)。但就表达式的真值而言,它等价于
find * -maxdepth 0 -name "efence*" -true
and since you didn't specify any other action, -print
is assumed (this assumption is always present as it allows you to type e.g. find . -name "*.java"
instead of find . -name "*.java" -print
).
并且由于您没有指定任何其他操作,-print
因此假设为(此假设始终存在,因为它允许您键入 egfind . -name "*.java"
而不是find . -name "*.java" -print
)。
Hope that makes sense. The accepted answer at the other threadtalks about the same thing.
希望这是有道理的。另一个线程中接受的答案谈论了同样的事情。