Using Unix's find command to find directories matching name but not subdirectories with same name

Edited: I mistakenly misrepresented my problem. A more accurate example now appears below.

I'd like to recursively walk all directories inside a target directory, and stop each recursive call after the first .git directory is found.

For example, if we have these paths:

/home/code/twitter/.git/
/home/code/twitter/some_file
/home/code/twitter/some_other_file 
/home/code/facebook/.git/
/home/code/facebook/another_file
/home/code/configs/.git/
/home/code/configs/some_module/.git/
/home/code/configs/another_module/.git/
/home/code/some/unknown/depth/until/this/git/dir/.git/
/home/code/some/unknown/depth/until/this/git/dir/some_file

I want only these lines in the result:

/home/code/twitter
/home/code/facebook
/home/code/configs
/home/code/some/unknown/depth/until/this/git/dir/

The -maxdepth won't help me here because I don't know how deep the first .git dir will be for each subdirectory of my target.

I thought find /home/code -type d -name .git -prune would do it, but it's not working for me. What am I missing?


Sounds like you want the -maxdepth option.

find /home/code -maxdepth 2 -type d -name .git


It's tricky and -maxdepth and recursion trickery won't help here, but here's what I would do:

find /home/code -type d -name ".git" | grep -v '\.git/'

In english: find me all directories named ".git" and filter out any occurences in the resultlist which contain ".git/" (dot git slash).

Above commandline will work on all unix systems, however, if you can assert that your find will be "GNU find", then this will also work:

find /home/code -type d -name ".git" ! -path "*/.git/*"

Have fun.


Find all directories which contain a .git subdirectory:

find /home/code -type d -name .git -exec dirname {} \;