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 Gitlab

You might also enjoy…

  • Render Conf 30th March 2017 - A review

    Posted on 18th May 2017. Written For Liquid Light

    On 30th March, I took the many trains over the many hours required to get to Oxford from Brighton and attended Render Conf. In it’s second year, this conference ...

Mike Street

Written by Mike Street

Mike is a front-end developer from Brighton, UK. He spends his time writing, cycling and coding. You can find Mike on Twitter.