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.
Soft vs. Hard links
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.
Soft links in action
Let’s say we have a file called “first_file.txt” that has four lines of text within it.
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.”
If we attempt to view the contents of “sl-firstfile.txt,” we can see it is the same as “first_file.txt.”
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.
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.
Deletion behavior of soft links
When the target file is deleted, the soft link will still be around.
However, it has becomes a “dangling” or “broken” link and is useless.
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.
The output of this command will be the absolute path of the original file.
Hard links in action
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 hl-firstfile.txt hl2-firstfile.txt
A check on both files shows that their contents are the same.
In the GUI, both files appear as distinct entities. There is no indication about the existence of a hard link.
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.
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.
When would soft links be useful?
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.
When would hard links be useful?
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.
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.
Our latest tutorials delivered straight to your inbox