防止 Unix "find" 命令递归到匹配的目录中

Prevent Unix "find" command from recursing into matched directories

考虑以下命令来查找和更新特定目录中的所有 git 存储库:

find . -type d -name .git | while read src_dir; do
    my_git_update "";
done;

假设一个存储库不能包含其他存储库,因此没有必要深入了解该目录。我没有使用 sub-modules 之类的东西,所以嵌套案例不算作这个问题。所以总的来说...

有没有办法告诉 find 在找到匹配项后停止递归到目录中?

根据我的搜索,答案可能是 -prune,但我不知道如何在我的案例中使用它。


尽管有标题,

Unix 'find' without descending into matched directories 不是 的副本。它着重于删除 sub-directories - 该解决方案不适用于此处。

你是对的,答案是-prune:

This primary always evaluates to true. It causes find to not descend into the current file. Note, the -prune primary has no effect if the -d option was specified.

-d-depth 在这个特定的 find 中本质上是同义词。

-print比较:

This primary always evaluates to true. It prints the pathname of the current file to standard output.

这意味着您以相同的方式使用它们:在测试之前的条件并确定您位于不应递归遍历的目录中后,只需 -prune 即可移至下一个目录。 (-d 使它无效的原因是 -dfind 代码已经 运行 目录中的所有内容,因此 p运行ing 步骤继续前进,无论如何都会发生。)