In the Linux operating system, every single thing is a file. Even the pointers to these files or links are also files. There are two types of links generally used in Linux: hard links and soft links. This article mainly revolves around the hard links. A hard link to a file has the same inode value as the original file. However, the hard links can only be created for files and not for the directories in Linux. Also, a hard link to a file can only be created within the same directory where the original file resides and not elsewhere. It means that the hard links cannot be created across different directories. In particular, we will show you the methods of creating and using the hard links in Linux.
How To Create Hard Links in Linux
To learn the creation of a hard link in Linux, you can go through the following steps:
Step # 1: Creating a Hard Link in Linux
For the sake of demonstration, we have created a file in the Home directory of our system named “dummy.txt”. We wish to create a hard link for this file with the name “dummy2”. To do so, we will use the following command:
$ ln –v /home/system/dummy.txt /home/system/dummy2
Here, the first path refers to the path of the source. The second path refers to the path of the link to be created. You can replace these paths according to your particular situation.
Upon the successful creation of the said hard link, you will get the following output:
Step # 2: Verifying the Creation of the Hard Link in Linux
Now, we will try to verify the creation of the said hard link, i.e., whether the inode value of the hard link is the same as that of the original file or not. To do so, we will run the following command:
$ ls –li /home/system/dummy.txt && ls –li /home/system/dummy2
You can verify from the following output that the inode value of both the files, i.e., the original one and the hard link, is the same, which implies that our hard link has successfully been created.
How To Use the Hard Links in Linux
After creating the hard link, we will play around with it a little. You can refer to the following examples shared for this purpose:
Example # 1: Checking the Status of the Hard Link After Renaming the Original File
In this example, we will try to modify the original file’s name and take a look at the status of the hard link, i.e., whether it is still pointing to the same file or not. For doing so, we have renamed our file “dummy.txt” as “test.txt”. After renaming this file, we executed the following command:
$ ls –li /home/system/test.txt && ls –li /home/system/dummy2
You can confirm from the output shown in the following image that the values of both the inodes, i.e., the inode value of the original file (after renaming it) and the hard link, are still the same, which means that renaming the original file leaves no impact on the hard link.
Example # 2: Checking the Status of the Hard Link After Deleting the Original File
Now, we will attempt to delete the original file and see how it affects the hard link. We deleted our original file, i.e., “test.txt” (after renaming). After deleting this file, we wanted to see if the hard link to this file still exists or not. For that, we used the following command:
$ ls –li /home/system/dummy2
The following output implies that not only the hard link to that file exists on our system after the deletion of the file, but it still has the same inode value as before.
Conclusion
This article was designed to keep the importance of the hard links in Linux. We explained the procedure of creating a hard link to a file in Linux. After that, we also shared with you some of the usage scenarios of the hard links. These scenarios will make the usage of the hard links quite easier for you.