CentOS Debian Mint openSUSE Red Hat Ubuntu

Hardlink and Softlink in Linux – Uses and Examples

Hardlink and Softlink in Linux

Hard links and softlinks are two different types of links that are very useful on Linux. Here, let’s discuss them with examples.

Hard Link:

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.

Soft Link:

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.

Keys difference between Hard Link and Soft Link

Hard Link Soft Link
Even if the original file gets deleted, it does not get deleted. If the original file gets deleted, then it will also get deleted.
Shares same inode number Shares different inode number
Mirror copy of the original file Definite link to the original file
Can link only files Can link both files and directories

Use cases of Hard Link on Linux

Let’s create a file named linuxways.txt and create a hard link to this file.

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

Inode numbers for both hardlinktest.txt and linuxways.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 linuxways.txt

Hard Link file still exists with the same content.

$ ls -li
$ cat hardlinktest.txt

It 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

Use cases of Soft Link on Linux

Let’s create a file named linuxways.txt and create a soft link to this file.

$ sudo mkdir softlink
$ cd softlink
$ sudo touch linuxways.txt
$ sudo ln -s linuxways.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.

Conclusion:

This is the way you create hard links and soft links also known as symbolic links. You must get the basic difference between them and the process of creating such links now. Thank you!

Similar Posts