:: introductory, computers

A quick introduction to symlinks. They are indispensable for configuring your computer!

On your computer’s file system you have files and directories (AKA folders). You also have some other things, including symlinks. The name "symlink" is short for symbolic link, and that is what they do – link things via symbols. A symlink contains just one thing (one symbol), which is a file path to some other thing (file, directory, another symlink...). When you open a file that is actually a symlink, your computer looks at the path that the symlink holds and accesses that file or directory instead. Why would you want to do this? So many reasons! Here are a few:

  • A configuration file needs to live at a specific place (eg. $HOME/.bashrc for your bash configuration), but you want it to live somewhere else (eg. a git repository or dropbox folder).

  • Sometimes you want a file to be in multiple places, but you don’t want to copy it (because you don’t want it to take twice as much space, and you want changes to show up in each place). One common example is for versioned libraries on Unix systems – you tend to have python be a symlink to python2, which is a symlink to python2.7, which may in turn be a symlink to python2.7.1. This allows you to have multiple versions of something installed with full version numbering, but if you don’t specify the full version you get the most recent applicable one.

  • Sometimes you put a file or directory in one place, then decide it should be somewhere else. But people or programs have come to expect it to be at the old place. You can make a symlink pointing the old place to the new place during a transition period.

The most important is the first one. Whenever I change a configuration file, I put that file into a git repository. This allows me to track changes to it and synchronize it between all my machines. This means that I can make elaborate customizations to my programs once, then use those customizations always on every computer I use.

To make a symlink on Unix, simply run the command ln -s <where the link should point to> <name of the link> in a terminal. Wonder why you need the -s? If you leave the -s off, you get a hard link. They do some similar things... but I think they’re less useful, and I never use them for anything. To make one on Windows, throw your computer off the roof of a tall building. Just kidding! You have to run a cmd prompt with administrative privileges, then run mklink /j <name of the link> <where the link should point to>. I remember I had some frustrations when I tried to use symlinks on Windows when I worked at a place that required it for certain things, but fortunately you can ditch Windows for a real operating system for personal use (and get a better job for business use).

One important note about symlinks: Always use relative paths. If a mount point changes, if you mount files remotely with sshfs, if you use a chroot, etc, links with absolute paths break. Relative links to anything within the same directory will always work. Relative links that ‘..‘ and point to something that is a parent of (or at any rate not in) the current directory are more likely to work than absolute links when dealing with the aforementioned situations.