December 18, 2018

Can't delete a symbolic link to a Linux directory!!

I've come across this issue once where I wanted to delete a symbolically linked directory (named foo for this example) on a CentOS box using the "rm" command, and I got this error message:

rm: cannot remove `foo/': Is a directory

which was a little frustrating although simple enough to resolve.

The mistake that led to generating this error message is that I used the autocomplete line by using the tab key to finish off the name of the directory, which is fine for most purposes, but in this particular instance of removing a symbolic link, it introduced an undesirable factor which is the forward slash tacked to the end of the directory name.
So the typed command looked like this: rm foo/ instead of just this rm foo

Basically, the RM command in bash refused to unlink the symbolic link because it saw it as a directory with content within.

So just in case someone else comes across this same issue, all you need to do is remove the trailing slash from the end of your command and the rm command will successfully remove your symbolically linked directory without any fuss.

Another approach is to use the "unlink" command followed by the name of your symbolic link and it'll also work just fine. Just note that the "unlink" command also will generate an error if it sees a trailing slash after the directory name.

So the correct syntax for removing a symbolically linked directory is this:

rm foo

Or

unlink foo

Share: