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.
# Folder already symlinked. Recreate symlink in case the directory changed.
# Possibly an existing, non-empty folder.