Update: the test -depth
with arguments (-depth 2
) is not specified in the documentation of GNU find
. It is probably an OSX extension. Don't use it!
Use -mindepth 2 -maxdepth 2
instead, as suggested by @hek2mgl in their answer.
OSX specific
It seems the OSX version of find
unnecessarily descends into directories deeper than 2 levels when -depth 2
is used (but this is the correct behaviour, see below).
You can tell it to not do that by adding -prune
immediately after -depth 2
(it seems it doesn't have any effect if you put it somewhere else):
find ~/repo -depth 2 -prune -type d -name .git
Some benchmarks:
$ time (find . -depth 4 -prune -type d -name .git | wc -l)
20
real 0m0.064s
user 0m0.009s
sys 0m0.046s
Moved -prune
at the end and it suddenly needs a lot of time to run:
$ time (find . -depth 4 -type d -name .git -prune | wc -l)
20
real 0m12.726s
user 0m0.325s
sys 0m9.298s
Remarks
On a second thought (and after a closer reading of man find
) -depth 2
does not require find
to stop descending in directories deeper than two levels. It can be part of a more complex condition that requires -depth 2
or something else (f.e. find . -depth 2 -or -name .git
).
To force it to stop descending more than two levels you must use either -maxdepth 2
or -depth 2 -prune
.
-maxdepth
tells it to not go deeper than two levels;
-depth 2 -prune
tells it to stop descending into subdirectories if the directory under examination is two levels deep.
They have equivalent behaviour, choosing one or another is a matter of preference. I would choose -maxdepth 2
because it is more clear.
Conclusion
Because -depth 2
is not portable, the final command should be like:
find ~/repo -mindepth 2 -maxdepth 2 -type d -name '.git' -print
Thanks @hek2mgl for mentioning about the compatibility issue.