×


Hardlink and Softlink in Linux – How it works with Examples

A link is a pointer to a file. Basically, links in Linux are pointers pointing to a file or a directory. Creating links is a kind of shortcuts to access a file. Links allow more than one file name to refer to the same file, elsewhere. 

There are two types of links : Soft Link or Symbolic links and Hard Links.

Here at Ibmi Media, as part of our Server Management Services, we regularly help our Customers to perform related Linux system queries.

In this context, we shall look into the differences between Hard links and softlinks and how they are very useful on Linux.


What does Hard Link mean ?

Hard link shares the same inodes as the original file. It is the mirror copy of the original file. It is only useful for files as it cannot create links for directories. If you change the content to either original or hard link file then changes occur on both. The main fact about hard link is that they do not get deleted even by deleting the original file.


What does Soft Link mean ?

Soft link is also known as symbolic link. It does not share the same inode as the original file. It is the definite link to the original file so if you remove the original file then it also gets removed. It can create links for both files and directories.


Hard Link features includes:
  • Even if the original file gets deleted, it does not get deleted.
  • Shares same inode number.
  • Mirror copy of the original file.
  • Can link only files.


Soft Link features includes:
  • If the original file gets deleted, then it will also get deleted.
  • Shares different inode number.
  • Definite link to the original file.
  • Can link both files and directories.


Example of using Hard Link on Linux

Let's create a file named linuxapt.txt and create a hard link to this file:

$ sudo mkdir hardlink
$ cd hardlink
$ sudo touch linuxapt.txt
$ sudo ln linuxapt.txt hardlinktest.txt
$ls -li

Inode numbers for both hardlinktest.txt and linuxapt.txt are the same i.e 393248 and same file permissions (-rw-r–r–). Hard link file does not get deleted even if the original file gets deleted:

$ sudo rm linuxapt.txt

Hard Link file still exists with the same content:

$ ls -li
$ cat hardlinktest.txt

This proves a hard link file does not get deleted even after deleting the original file. If the hard link file needs to be deleted then you must delete it with the following command:

$ sudo rm hardlinkfile.txt


Example of using Soft Link on Linux

Let's create a file named linuxapt.txt and create a soft link to this file:

$ sudo mkdir softlink
$ cd softlink
$ sudo touch linuxapt.txt
$ sudo ln -s linuxapt.txt softlinktest.txt
$ ls -l

Here, inode numbers are different for the original and soft link file, also you can see different file permissions. If you delete the original file then the soft link file also gets deleted as it is the actual copy of the original file.


[Need assistance in fixing Linux system permission issues ? We can help you. ]


Conclusion

This article covers how both hard links and soft links also known as symbolic links works in a Linux system. In fact, Soft links, also called symbolic links, are files that points to other files on the filesystem while Hard links to a file are instances of the file under a different name on the filesystem.


What are inodes?

Inodes are essentially identification cards for your file. They contain the file metadata, the file permissions, the file type, the file size but most importantly the physical address of this file on the disk.