I got this question in an email the other day, and I realized it’s something many people might be unfamiliar with. In Linux, there are two types of file links, hardlinks and softlinks. Here, I’ll give a quick explanation of what these types of links are, and when you should use them.
What is a softlink?
Softlinks, also called symlinks, are the easiest to understand, especially because you’ve probably already used them. A softlink fills the same role as a Windows shortcut. Simply put, a softlink is a file that points to another file. When you create a softlink, you are creating a new file that exists only as a pointer to a file elsewhere on the system.
All links are created with the ln command, using the -s flag if you want a softlink (if you don’t use -s, you’ll get a hardlink, which I’ll talk about in a second). The syntax of the command is:
ln -s [target] [link name]
For example, the following command will create a softlink to /usr/bin/firefox called firefox (in my “Desktop” directory):
ln -s /usr/bin/firefox ~/Desktop/firefox
You can see the softlink’s target by using the ls -l command. You can also use this command to detect broken softlinks:
Now that we understand softlinks, let’s talk about hardlinks.
What is a hardlink?
Whereas a softlink is a new file that points to an already-existing file, a hardlink is another instance of the original file.
A diagram is the easiest way to explain what that means:
You can click on either of the images for a larger version. What it explains is that, when you create a hardlink, you are creating another pointer to the data location on disk, not a pointer to the existing file. That means that editing a hard link of a file is equivalent to editing the original instance of the file.
To drive the point home: a softlink is a shortcut to an existing file, whereas a hardlink is a reference to a location on disk (or, more accurately, a location in the filesystem). This means that the concept of a shortcut, a link pointing to another file, doesn’t make sense for hardlinks. But, what does make sense is asking how many references exist to a given location on disk (how many hardlinks exist for a file), which you can see by running the ‘stat’ command:
So for example, in this screenshot I’ve created a file and then built three hardlinks to it. When I run the ‘stat’ command on any of the files, it will show that there is a reference count of 4:
When to use softlinks
There are two major limitations of hardlinks. In these cases, you must use a softlink:
1. A link across filesystems
Because a hardlink is a direct reference to the underlying filesystem, you can’t hardlink across filesystems.
2. Linking to a directory
You can’t use a hardlink to link to a directory.
In the cases listed above, you must use a softlink because a hardlink simply won’t work. There are also cases where softlinks are preferable to hardlinks, even though either will work. For example, you’d probably want to pick a softlink when you want to create a shortcut especially when the destination of that shortcut might change in the future. For example, if you have a launcher to a beta version of an application, you may wish to use a softlink so you can easily change the target of the link between versions.
In most of the remaining cases (creating a link to a file on the same filesystem), hardlinks can be preferable for the following major reasons:
There is a slight performance boost to be gained from using hardlinks. This is because since a hardlink references a spot on disk, rather than referencing another another file (which then references the actual spot on disk you want), there is one less disk seek involved in hardlinking.
2. Storage space
Hardlinks don’t take up any additional space, since they are simply a reference to an already existing space of disk. Creating a softlink is creating a new file, and will consume a small amount (usually 4KB) of space on your filesystem.
Additionally, hardlinks are preferable because a softlink is vulnerable to losing the ‘master’ instance of a file (the file to which all the softlinks point). If the original file is deleted, then all softlinks to it become invalid. With a hardlink, you can delete any of the instances, including the original, and as long as at least one instance exists the file will remain on the system.