Stupid Lessons #2: Working with Symlinks

I managed to do it again - I deleted the files I was not intending to do. Back-ups to the rescue again! Last time it happened due to incorrect understanding of SAS temporary and permanent libraries, and this time due to incorrect understanding of symlinks.

Symlink i.e. symbolic link, is a reference to another file or directory. Once the symlink is set up, you can just type cd my-simlink in your terminal rather than cd path/to/my/directory/ to navigate to your frequently accessed directory. If your files go many layers deep, having symlinks is very convenient.

Symlinks are very easy to set up. All you have to do is type ln -s path/to/my/directory/ and the symlink will appear in the directory you are currently in. Then you can just type cd directory rather than the entire path to navigate to the set destination.

The pwd will show that you are now in /directory rather than path/to/my/directory but in reality you are still in the same physical location and any changes you make will be stored permanently no matter if you navigate to your destination through complete path or a symlink. The entire procecure can be seen below:

symlink usage

Never do this

This section shows how I deleted the files I was not intending to so you, and I don't do it again. The error is quite subtle. Look at the screen recording below and see if you can make a guess of what I have done (remember that directory contains a file hooray.txt)

Incorrectly deleting symlink

If you guessed that I tried deleting a symlink with rm directory/ and that's what caused an error, you got it right. If you just type rm directory without a forward slash in the back, then it removes a symlink. Otherwise, if you leave the forward slash, it removes all the contents within the directory.

To correctly unlink symlinks, just use unlink symlink i.e. unlink directory command, or rm symlink i.e. unlink directory without the forward slash.

That's it, another tough lesson learnt. What's coming up next?