当我做:

$ find / 

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

(这个问题来自“回答“到另一个问题。)

有帮助吗?

解决方案

你好,

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

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

这将阻止 find 下降到该目录中的子目录。

基本上,它说,“修剪任何名称不是“.”的目录,即当前目录。”

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

HTH。

欢呼,罗布

其他提示

考虑:

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

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

$ ls *.ksh

使用 -修剪 选项。

你甚至可以这样做

echo /specific/dir/*.jpg

因为是你的 shell 扩展了通配符。打字

ls *.jpg

相当于打字

ls foo.jpg bar.jpg

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top