Linux – Using Unix’s find command to find directories matching name but not subdirectories with same name

command linefindlinuxunix

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:


I want only these lines in the result:


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?

Best Answer

Sounds like you want the -maxdepth option.

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

Related Question