Oct12
What Are Hard And Soft Links?

What Are Hard And Soft Links?

One of the features of the file systems used by Linux and Unix systems is the ability to create links for files. There are two types of links: soft links (also called symbolic or shortened to sym) and hard links. Both can be used to link to a file from another location, but the way they go about it is different.

Hard Links

First we’ll look at hard links. Linux file systems allow files to have more than one filename. A hard link is the name given to that association between a filename and a file’s contents. A file can have as many hard links to its contents as you desire but all those hard links have to be on the file system where the contents are stored. When all of the hard links to a file’s contents are deleted then that data is lost, otherwise the hard links can be deleted (including the one that the file was created as) without losing the data.

In general, though, when referencing hard links people mostly mean making additional hard links to a file beyond its original one. Linux provides a command called “ln” which is used to create links, and by default this creates hard links. It can be used as follows:

ln <source file> <destination file>

When specifying the files you can use either full paths or relative paths as required:

ln test.file ~/test2.file

The above command would make a hard link in the user’s home directory named test2.file to the file named test.file in the current working directory.

Soft Links

So now let’s look at soft links.These are slightly different to hard links. While a hard link links the new file name directly to the contents of the file, a soft link simply links to another file name. Imagine it a bit like a redirection, so when you open a soft link it tells your software to open the contents of the file it links to instead. One advantage of a soft link is that you can link to files on different file systems to the one that the link is being created on. The main disadvantage to using a soft link is that if the original file is ever deleted or moved then the link will no longer work.

Soft links are often used on Linux systems to specify which application to use when there are multiple alternative options. For example, most Linux systems ship with Python 2 and Python 3. The file /usr/bin/python will be a symlink to one of those versions of Python dependant on the chosen system default.

To create a soft link, you can use the same ln command similar to before, but with the -s flag to specify the soft link:

ln -s test.file ~/test2.file

A practical example where you may see soft links in use would be when using Apache configuration on a Debian/Ubuntu based system where configuration for virtualhosts is created in a sites-available directory and mods in a mods-available directory. Using the a2enmod and a2ensite tools to enable a virtual host or mod creates a soft link for you in the sites-enabled/mods-enabled directory.

You can use ls with the -l flag to see the target of a soft link in a particular directory:

ls -l

With a soft linked file you’d see an output similar to:

lrwxrwxrwx 1 root root  9 Jun  4  2016 python -> python2.7
Something to note is that hard links can only be used to link to directories by the superuser because of the potential issues that can be caused by creating a recursive loop on the file system. It is recommended only to use soft links to link to directories as most applications can handle them transparently whilst allowing them to understand the nature of the link.

This article was brought to you by VPS.net, for dedicated server hosting, cloud servers and 24/7 support visit our site here vps.net

No Comments

over 200,000 servers launched

and counting worldwide...