有什么方法可以防止find递归地挖掘子目录?

时间:2020-03-05 18:43:04  来源:igfitidea点击:

当我做:

$ find /

它搜索整个系统。
我该如何预防?

(此问题来自另一个问题的"答案"。)

解决方案

回答

使用-prune选项。

回答

使用通配符可能会更好。例如,如果要在当前目录中查找所有ksh脚本:

$ ls *.ksh

回答

考虑:

-maxdepth n
             True if the depth of the current file into the tree is less than
             or equal to n.

-mindepth n
             True if the depth of the current file into the tree is greater
             than or equal to n.

回答

你甚至可以做

echo /specific/dir/*.jpg

因为是外壳扩展了通配符。打字

ls *.jpg

相当于打字

ls foo.jpg bar.jpg

给定foo.jpg和bar.jpg是当前目录中所有以" .jpg"结尾的文件。

回答

G'day,

只是想扩展Jon的建议以使用-prune。这不是最容易使用的查找选项,例如,仅在当前目录中搜索查找命令,如下所示:

find . \( -type d ! -name . -prune \) -o \( <the bit you want to look for> \)

这将阻止查找到该目录的子目录。

基本上,它说:"修剪目录名称不为"。"(即当前目录)的任何内容。"

对于当前目录中找到的每个项目,查找命令从左到右执行,因此在完成第一个元素(即修剪段)后,它将继续在第二个-o(OR'd)表达式中继续显示匹配的项目。

HTH。

干杯,