Deleting directories while preserving symlinks

At work, we needed to replace a pre-existing folder with a symlink. We wanted to symlink the user’s .git/hooks folder to ../build/git-hooks every time the script was run.

We wanted to delete .git/hooks if it existed and replace it with a symlink, but we also wanted to avoid deleting ../build/git-hooks‘s contents by accident if the symlink already existed.

Using rm .git/hooks was not possible. If .git/hooks was already a symlink, it would be deleted and we could simply recreate it. Perfect! However, if .git/hooks was a non-empty directory (it contains examples by default), it would not be deleted, and the symlink could not be created.

Using rm -r .git/hooks was also impossible. If .git/hooks was already symlinked, the -r flag would delete the contents of ../build/git-hooks.

This is how we solved the problem:

set -e  # Script exits with 1 on error


# Safely delete the folder or symlink
if [[ -L $git_hooks_dir ]];
    # Folder already symlinked. Recreate symlink in case the directory changed.
    rm -f $git_hooks_dir
    # Possibly an existing, non-empty folder. 
    rm -rf $git_hooks_dir

# Create the symlink
ln -sf $link_to $git_hooks_dir

Leave a Reply

Your email address will not be published. Required fields are marked *

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    Markdown is turned off in code blocks:
     [This is not a link](

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see