Hard links

A hard link in Linux is a duplicate directory entry. Both directory entries point to the same file (or more precisely, to the same inode). Neither entry holds any sort of priority over the other and both are equally valid. To delete the file, you must delete both hard links to the file.

To create a hard link, you can use the ln command without any options. You need to specify the filename and the name of the link:

linux creating hard linux

In the example above we have created a hard link called hlink.lnk to the file original_file.txt. Note that both files have the same inode number.

Now we will change the content of the file and then display the new content using different filenames:

linux hard links change content

Because both directory entries point to the same file, both filenames can be used to access the same content.

To delete a file, you must delete both directory entries:

linux deleting hard links

In the picture above you can see that, although we have deleted the original_file.txt, the file still exists.

Hard links can only be created to files on the same partition.
Geek University 2022