[PSA] Working with symbolic links? Don't make this dumb mistake!

Difficulty: ☆☆☆☆☆

There is one character which differentiates between deleting symbolic links, and deleting everything: /

When deleting symbolic links via rm, DO NOT EVER end directories with a forward slash. Instead of deleting the link, it deletes the files inside the linked directory!

I cannot stress this enough. I lost nothing important when I made this mistake, but it’s a lesson worth knowing before your script destroys years of work if you’re deleting symbolic links in active use!

How would you know if you screwed up? If the time it took before control of the terminal is returned wasn’t enough of a clue, you can use -v with rm for verbose output of what it’s doing.

A safer alternative

Rather than use rm for everything and be a lazy coder, the responsible thing to do is to use unlink instead. As symlinked directories are not directories the rmdir command does not work!

:brain: Previously I said rmdir does work. in my rush to bang this out I lost my brain along the way. Thank you, @Fabby for pointing this out and finding a way to mash it back into my skull.


Actually, you should use


to remove symbolic links… This will not protect you from accidental file deletion, but it will protect you from directory deletion:

$ cd /tmp
$ md dirfoo
$ ln --symbolic dirfoo lnfoobar
$ unlink lnfoobar/
unlink: cannot unlink 'lnfoobar/': No such file or directory



Fixed, thanks.

1 Like

There is a way to secure you against file deletion. When using (readonly) snapshots You can recover all your files from the last snapshot. This does come with a cost. You have to use a filesystem like btrfs and an utility that automatically takes snapshots like snapper, and you will need some additional space on your drive. But i think it is worth the work. In the last years i did not loose a single file (only because i could recover them every time from the snapshots)

This is no replacement for a good backup (lightning may strike, or your disk may break), but an addition that comes handy in daily work.

Snapshots are a good addition to backups, especially for recovering accidentally deleted files because:

  • they are fast to access
  • every user on the machine is able to recover his own files easily
  • they are readonly (you can’t mess them up accidentally)
  • they are taken with no delay (only cleaning them up needs some time)
  • they take only a small amount of filespace, because they are in place
  • they do include ALL files (even .something , links , permissions …), so you don’t have to maintain a list of files or folders
  • snapper does this automatically (you can’t forget), even on boot, or on update (when configured so). On my system taken once every hour.
  • you even can make fast incremental external backups from snapshots with btrfs. (When restored this will be bootable)
  • These external backups of snapshots are mountable. Then you are able to browse them like every other snapshot in your running system.

good backup: best kept disconnected from the working PC
snapshots: inside the working PC


Wow I did not know that existed.

I’m pretty sure I just always used rm and never had a problem.

you need to use rm -r to recursively delete everything inside a directory as far as I’m aware, so just rm /path/to/symlink should delete the symlink and rm /path/to/symlink/ should… well honestly idk let me check to be sure…

$ rm slink/
rm: cannot remove 'slink/': Is a directory


But I suppose unlink does appear safer even if I never messed it up before.

I think I’d have to actually make that mistake to start using unlink though, rm is already a habit now :grimacing: