rm -rf

Deleting symlinks aka “Don’t drink and sudo”.

Tuesday, September 13th, 2016 | Insights, Linux, Tech-savvy | No Comments

failcatWell, I did neither, yet still fucked up my installation the other day. Here is how I managed it – and yes, there is some knowledge sharing / advice in here, so read on.

I guess everybody who ever came across a *nix prompt knows the infamous “rm -rf” command and only uses it when it’s really save. Well, pretty much save and then whoopsy … files gone. So here goes my latest foobar.

I wanted to create a symlink from my small home partition to a big data portion for my gnome-boxes, as disk images tend to get big. So I created and “cd”ed into the destination folder:

cd /bfd/gnome-boxes/

and checked whether the dir is empty thus suitable. It was.

So now I symlinked the current directory to ~/.local/share/gnome-boxes

ln -s . /home/nick/.local/share-gnome-boxes/

So far so good. I “cd”ed into the original folder to check whether the link was created okay, but it only showed something like

gnome-boxes -> .

instead of

gnome-boxes -> /bfd/gnome-boxes/

WTF? The . is a reference to the current directory which was /bfd/gnome-boxes at the time I used it. Hmm, the ln took that dot pretty literally (which is obviously my fault).

So, I wanted to get rid of that wrong symlink. First, as the “directory” gnome-boxes was a symlink, i.e. a file, I tried to simply delete it with rm. That was when the real fun began.

You can test this behavior, create a test directory, that holds a file (touch file) and a directory (mkdir dir). Then go create that bogus symlink (ln -s . ./test)

drwxrwxr-x. 2 nick nick 4096 13. Sep 12:50 dir
-rw-rw-r--. 1 nick nick    0 13. Sep 12:53 file
lrwxrwxrwx. 1 nick nick    1 13. Sep 12:53 test -> .
[nick@localhost 1]$ rm test/
rm: das Entfernen von 'test/' ist nicht möglich: Is a directory
[nick@localhost 1]$ rmdir test/
rmdir: konnte 'test/' nicht entfernen: Not a directory
[nick@localhost 1]$ rm -rf test/
[nick@localhost 1]$ ll
insgesamt 0

All gone 🙂

Not only the dir was gone but basically my whole ~/.local/share/ directory was empty. I symlinked gnome-boxes to ., so the current dir, thus rm -rf gnome-boxes deleted the current dir. Eff.

How to prevent this.

* Use unlink. It’s saver as it only calls the unlink function and cannot delete regular files.
* Don’t “tab” complete your commands. A simple “rm gnome-boxes” without the trailing slash would have done the job and saved me the re-installation. Luckily this is done in less than half an our with modern distros, still, I felt like an idiot. Lesson learned.

This was done with a super smart ASF member behind me, whom I showed the funny “Is a directory / Not a directory” error messages. We had a good laugh after deleting my share folder. Facepalm deluxe!

Tags: ,

Search

Categories