Removing symbolic links to directories

Having trouble removing a symlink from your POSIX compliant (Linux/Unix/OSX/MyDogSamix) system?  A quick Google yields some interesting results, which may or may not be helpful.

Most likely, you’ll find a bunch of condescending links highlighting the fact that you can just ‘rm <symlink>’ the symbolic link.  Silly newb. However, if this is a directory, you might have problems.

The most important thing to remember is that to remove a symlink that points to a directory, you must not include the trailing slash.

In other words:

[fdev@warproof sites]$ ls ../sites/
all  default  sites
[fdev@warproof sites]$ rm sites/
rm: cannot remove `sites/': Not a directory
[fdev@warproof sites]$ rm sites
[fdev@warproof sites]$ ls
all  default

Notice that the first attempt (‘rm sites/’) failed, while the second attempt (‘rm sites’) did not. YMMV, but in this case, I blame my autocompletion (aka tab completion) – it automatically adds the trailing slash. Remove that slash, and you’ll remove the symlink.

May all your deletions be error-free.

One thought on “Removing symbolic links to directories”

Comments are closed.