5333 private links
rm typically does not delete the targets of symlinks, but to say that it "does not follow symlinks" is not quite accurate, at least on my system (GNU coreutils 8.25). And deleting files is a place where accuracy is pretty important! Let's take a look at how it behaves in a few situations.
If your symlink is to a file, rather than to a directory, there is no plausible way to accidentally delete the target using rm. You would have to do something very explicit like rm "$(readlink file)".
Symlinks to directories, however, get a bit dicey, as you saw when you accidentally deleted one.
These are all safe:
rm test2
(deletes the symlink only)rm -r test2
(deletes the symlink only)rm -rf test2
(deletes the symlink only)rm test2/
(rm: cannot remove 'test2/'
: Is a directory -- no action taken)rm -rf *2
(or any other glob matching the symlink -- deletes the symlink only)
These are not safe:
rm -r test2/
(rm: cannot remove 'test2/'
: Not a directory -- but deletes the contents of thetest1
directory)rm -rf test2/
(deletes the contents of the directory, leaves the symlink, no error)rm -rf test2/*
(deletes the contents of the directory, leaves the symlink, no error)
The last unsafe case is probably obvious behavior, at least to someone well-acquainted with the shell, but the two before it are quite a bit more subtle and dangerous, especially since tab-completing the name of test2
will drop the trailing slash in for you!
It's interesting to note that test
has similar behavior, considering a symlink to a directory with a trailing slash to be not a symlink but a directory, while a symlink without a trailing slash is both: