Symbolic and Hard links in Linux

All you need to know about symbolic and hard links

I am sure you all have used some GUI-based Operating System once in your life and if that is the case then you might be familiar with the concept of shortcuts. They are typically located on your desktop or within a startup window menu. A shortcut is essentially a file that links to your application so you don't have to go to the installation location every time you want to open it.

In Linux, we can link files to other files, giving us the ability to create our shortcuts. There are two different ways to create links i.e hard and symbolic. They achieve the same purpose but both of them work differently and have their pros and cons.

In this article, we'll go over both of them, how they differ from one another and why they are useful to know for sysadmins jobs. So, let's get started.

Understanding Inodes

Before we go any further, we need to understand the concept of inodes. We won't be going into depth as it will be the topic for some other time. So, Inodes are data objects that store the metadata regarding files and directories such as time stamps, owner of the file, permissions, last modified date, etc. Inodes are represented by an integer number often times called the inode address.

To view the inode address, first, create two dummy files and name them sample-file-1.txt and sample-file-2.txt.

touch sample-file-1.txt sample-file-2.txt

Now to view the inode address use the ls command with the -i flag.

ls -i

Checking inode address

As you can see, the inode address of both of the files is 4203984 and 4203990. The inode address depends upon the filesystem and so it might be different in your case.

A hard link looks like a new file but points back to the data in the original file. They are duplicate entries, where both entries point to the same data. When you are creating a hard link, you're creating another file that points to the exact same data as the original file. It's just a carbon copy of the original file.

Now let's go over how to create a hard link

ln sample-file-1.txt link-1.txt

Here, I'm creating a hard link link-1.txt that is linked to the sample-file-1.txt.

To verify if indeed link-1.txt points to the sample-file-1.txt:

ls -i

Output after creating hard links

In the above output, the important thing to note is that sample-file-1.txt and link-1.txt have the same inode address which is proof that the hard link that we created points to the original file. With this hard link created, we can move link-1.txt to another location and it will still work.

Hard links are very easy to set up and use but this simplicity has its own shortcomings. First, you can only create a hard link for files and not for directories. Secondly, hard links can not be moved to a different filesystem as hard links depend upon the inode address and each filesystem has its own inode address.

To overcome this limitation, we have another type of link i.e symbolic links or soft links. As you might recall, hard links are duplicate entry that points to the same inode address. But, a symbolic link is an entry that points to another directory or file. This is different from the hard link as it only references a specific path.

To create a soft link use the following command:

ln -s sample-file-2.txt link-2.txt
ls -i

Output after creating soft links

Notice that the inode addresses are different. Symbolic links are not duplicate entries of the original file but the new file that points to the original file path. It works exactly in the same way as pointers work in C.

There are several advantages of using soft links such as it works for both files and directories. You can move soft links to a different file system and it will work as expected as it does not have any dependency on inodes.

Symbolic links have some limitations as well. As the symbolic link is just a pointer to the original file, if you move the original file or directory to another location, your link will break as the symbolic link won't be able to find the original file path. Another thing to note is that symbolic links don't inherit the original file permissions and this means changes to the original file permissions won't be reflected in a symbolic link.

Wrap up

As we have covered both hard and symbolic links, you might be wondering which one to use. The main benefit of the hard link is that you can move either the original file or the link to any different location and it will still work. This is not true for the symbolic links as it is just a pointer to the original file location. If you delete the original file, the hard link will still work but the symbolic link will break. The reason is straightforward, hard links are duplicates of the original file while symbolic links are just merely a pointer to the actual file path.

Taking into account the benefits of hard links versus soft links, you should prefer hard links only when you are sure that you might need to move links to different locations. Otherwise, you should prefer using symbolic links as they work with both files and directories and are filesystem independent.

This is it for this article, I hope you gained something valuable out of it. If you like this article and would like me to continue writing these types of articles then please give it a thumbs up. If you have any suggestions or questions feel free to use the comment section below.