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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 05:23:48  来源:igfitidea点击:

What does -prune option in find do?

linuxfind

提问by kingsmasher1

I can see the -prune of findnot working correctly. I guess -name "efence*" -pruneoption 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 pruneoption doesprint matching files, if no other options are specified (it still prevents findfrom recursing into matching directories, however).

如果没有指定其他选项,该prune选项打印匹配的文件(但是,它仍然可以防止find递归到匹配的目录中)。

Edited to add explanation:

编辑添加解释:

findexpressions distinguish between testsand actions. From man find:

find表达式区分testsactions。来自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. -andis assumed where the operator is omitted.

If the expression contains no actions other than -prune, -printis performed on all files for which the expression is true.[my emphasis]

表达式由选项(影响整体操作而不是特定文件的处理,并始终返回 true)、测试(返回 true 或 false 值)和操作(具有副作用并返回 true 或false 值),全部由运算符分隔。 -and假定省略了运算符。

如果表达式不包含除 之外的任何操作-prune-print则对表达式为真的所有文件执行。[我的重点]

So -pruneis an action which has the side effect that findwill 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, -printis 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.

希望这是有道理的。另一个线程中接受的答案谈论了同样的事情。