Understanding the Differences Between Soft and Hard Links in Linux

Bg1

On your working Linux system, let’s say you have a file nested within multiple layers of directories. In order to easily access that file, you wish to create a shortcut for that file on your desktop. It is possible to use symlink (also known as soft link). There is also another form of link known as the hard link. In this article we will explain the differences between soft and hard links and take a look at which one you should use.

A soft link, or symlink, is just a symbolic link. It is simply a pointer to the target filepath. A soft link doesn’t care if the file or folder at the filepath really exists. If the file or folder doesn’t exist, it will just result in a broken symlink. You can edit or delete the symlink, and it won’t affect the target file.

On the other hand, a hard link has the effect of giving the target file an additional name. When you edit the content on one file, it will modify the content on the other file as well. In short, a soft link is just a reference to the target filepath, while a hard link is a direct link to the data of the target file.

Below we will illustrate how soft and hard links work.

Let’s say we have a file called “first_file.txt” that has four lines of text within it.

Link Fileview

We will create a soft link in our Home directory.

ln -s first_file.txt ~/sl-firstfile.txt

In the GNOME Desktop Environment, a soft link can be created for a file by simply right-clicking the file and selecting “Create Link.” A soft link is created where the name is similar to “Link to <file_name>.txt.”

Link Sl Guicreate

If we attempt to view the contents of “sl-firstfile.txt,” we can see it is the same as “first_file.txt.”

Link Sl View

If we take a look at these two files in the File Manager, the soft link appears as a shortcut to the original file. The black arrow in the file’s icon denotes this.

Link Slfirst

When we attempt to view the current directory’s listing using the command ls -l, you can see where the soft link is pointing to the target file. The first character in the permission string for “sl-firstfile.txt” is “l,” which means this entry is for a link.

Link Slsecond

Also read: How to Manage Symlinks in Linux

Deletion behavior of soft links

When the target file is deleted, the soft link will still be around.

Link Sl Delorig

However, it has becomes a “dangling” or “broken” link and is useless.

Link Delview

Now, if you create another file with the same name – “first_file.txt” – the soft link will become active again.

Note: in case you encounter soft links and wish to find the absolute path of the file the link has been created for, simply use the readlink command as shown below.

Link Readlink

The output of this command will be the absolute path of the original file.

Now we will create a hard link for the same file in our Home directory. This can be done with the following command:

ln first_file.txt hl-first-file.txt

Note: the methods for creating the soft and hard link both use the ln command. The only difference is the -s flag which denotes a symbolic link.

Another way to create a hard link is by using the link command.

link hl-firstfile.txt hl2-firstfile.txt

A check on both files shows that their contents are the same.

Link Firstview
Link Hl Secondview

In the GUI, both files appear as distinct entities. There is no indication about the existence of a hard link.

Link Hlview

Let us attempt to add a new line to the hard link file “hl-firstfile.txt.”

echo "hello" >> hl-first-file.txt

The change in data can be viewed in both files.

Link Link Hlappview2
Link Hl Appview1

When a hard link is created, it is as if two different files exist, but their data is linked. A change in one is reflected in the other.

Quick tip: when creating soft links and hard links, it would be a good idea to use the same extension as the file the link is being created for.

Deletion behavior of hard links

For hard links, deletion of the original file or the hard link is not a matter of concern. The data that was initially stored on the file is still accessible through the hard link.

Link Hl Del
Link Hl Delview

We noticed that soft links appear as shortcuts for a file. When we have a file within multiple layers of directories and wish to store it in a location for easy access, a good option would be to simply create a short link for it.

We have seen that once a hard link has been created for a file, any change to the file or the hard link is reflected in both.

Let’s say you have a backup server. An incremental backup mechanism is being used where only the changes to files/folders are reflected since the last backup. You can create a hard link between a file in the server and the corresponding copy in the computer. When the backup happens, changes to the file in the computer would automatically be reflected to the corresponding hard-linked file in the server, even if the files undergo a name change.

Conclusion

Both soft and hard links have their own uses and should be used in different situations. Understanding their differences allow you to make better decisions when creating links. Soft links and hard links can be created for directories, too. While soft links can be created across filesystems, hard links can only be created within the same filesystem.

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox

Divya Lakshmanan

Divya divides her time between speculating the existence of aliens and writing about her technical findings.