Find folders on a linux server that do not have a file

At work we have a post-merge hook which fires whenever a merge is completed. This hook asks the user if they want to delete the branch they are merging from. Since this was set up, there have been a few sites that have escaped having this installed, so I wanted to locate all the .git/hooks folders without this particular file:

find /www/*/*/.git/hooks -maxdepth 0 -mindepth 0 -type d | while read dir; do [[ ! -f $dir/post-merge ]] && echo "$dir"; done

The * are wildcards, meaning find any folder in the www folder, which contains any folder, which contains a .git/hooks folder.

This was great and printed any hooks folder without the post-merge file. We also have the $dir variable now available should we need to do any processing.

In this next command, we symlink the post-merge hook and make it executable:

find /www/*/*/.git/hooks -maxdepth 0 -mindepth 0 -type d | while read dir; do [[ ! -f $dir/post-merge ]] && ln -s $dir/post-merge /path/to/shared/post-merge && sudo chmod +x $dir/post-merge; done

View this post on Github

You might also enjoy…

Mike Street

Written by Mike Street

Mike is a CTO and Lead Developer from Brighton, UK. He spends his time writing, cycling and coding. You can find Mike on Mastodon.