有没有办法防止find命令递归地查找子目录?

4

当我执行以下操作时:

$ find / 

它会搜索整个系统。
我该如何防止这种情况发生?

(这个问题来自于对另一个问题的"回答"。)

5个回答

10

考虑:

-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.

2
请注意,-maxdepth和-mindepth选项不可移植。GNU find具有这些选项,如果需要可以安装。 - Jon Ericson

4

你好,

我想进一步解释Jon提出的使用-prune的建议。这并不是最容易使用的find选项之一,例如,如果要在当前目录中搜索,find命令看起来像:

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

这将阻止查找进入此目录中的子目录。

基本上,它的意思是,“修剪任何名称不是“。”(即当前目录)的目录。”

find命令从左到右对当前目录中找到的每个项目进行评估,因此在完成第一个元素(即修剪部分)后,它将继续使用第二个-o(OR'd)表达式中匹配的项目。

希望对你有所帮助。

祝好, Rob


3

您最好使用通配符。例如,如果您想在当前目录中查找所有的ksh脚本:

$ ls *.ksh

0

你甚至可以这样做

echo /specific/dir/*.jpg

因为通配符的展开是由您的 shell 完成的。输入

ls *.jpg

相当于输入

ls foo.jpg bar.jpg

假设foo.jpg和bar.jpg是当前目录中以“.jpg”结尾的所有文件。


0

使用-prune选项。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接