I needed to go through a directory structure and do a git-fetch if there was a .git repository inside. Finding all directories containing the .git is quite easy with find command. find $root-directory -regex '^.*/.git$' -printf '%h\n' | sort -d This is nice but it also prints unneccesary data. It does not only print the leaf directories but also all the partial paths to them. The original project structure is something like: