0

I want to:

  • find all directories that start with a capital letter
  • then, find all directories named '.git' in those directories

This is for performance optimization. It's my solution to reduce the search scope to specific directories first, then search inside them.

For the first one, I have this in mind:

find / -mindepth 1 -maxdepth 1 -name "[[:upper:]]*"

And for the second one, I was using:

find / -type d -name ".git" 2>/dev/null

Now I don't know how to chain them together. I want the second find to only search in the results of the first find.

Or if it's an X-Y problem, please tell me your suggestions to increase the speed.

I require to find all .git directories on a machine. But it's guaranteed that .git directories are all inside top-level directories that start with a capital letter.

1
  • @KamilMaciorowski, can you elaborate more? Aug 11, 2022 at 7:19

2 Answers 2

4

Here, you can do it with one find expression and even avoid the -mindepth / -maxdepth GNUisms with:

find / ! -path / ! -path '/[[:upper:]]*' -prune -o -name .git -type d -print

More generally, you can chain find commands portably with:

find ... -exec sh -c '
  find "$@" ... -exec ... {\} ...' sh {} +

(here showing how to insert a {} for the inner (second) find to avoid it being picked up by the outer (first) one).

Or with recent versions of GNU find (4.9.0 or above):

find ... -print0 | find -files0-from - ...
1
  • I didn't understand the first single find you wrote. It says not / and not uppercase? The second ! is for uppercase? And can you please explain what -prune does here? Aug 11, 2022 at 9:41
4

Since the first find is only searching a single level, you can replace it with a simple shell wildcard pattern: /[[:upper:]]*. Then use that as the places for find to search:

find /[[:upper:]]* -type d -name ".git" 2>/dev/null
2
  • Have you tried it? It does not work. Aug 11, 2022 at 9:39
  • @SaeedNeamati I don't have a setup to test it exactly (i.e. with uppercase top-level directories), but with subdirs and ./[[:upper:]]* it works as expected. If it's not working for you, I'd try replacing find with echo, and make sure the wildcard is expanding properly. If that's not the problem, remove the 2>/dev/null and see if any errors are informative. Aug 11, 2022 at 9:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .