Every file on the Linux filesystem starts with a single hard link. The link is between the filename and the actual data stored on the filesystem. Even if you delete the original file, the hard link will still has the data of the original file because the hard link acts as a mirror copy of the original file. In this guide, we will see how to create and manage hard links

creating hard links

When we create a hard link to a file with ln, an extra entry is added in the directory. A new file name is mapped to an existing inode.

Both files have the same inode, so they will always have the same permissions and the same owner. Both files will have the same content. Actually, both files are equal now, meaning you can safely remove the original file, the hardlinked file will remain. The inode contains a counter, counting the number of hard links to itself. When the counter drops to zero, then the inode is emptied.

finding hard links

You can use the find command to look for files with a certain inode. The screenshot below shows how to search for all filenames that point to inode 674162. Remember that an inode number is unique to its partition.

symbolic links

Symbolic links (sometimes called soft links) do not link to inodes, but create a name to name mapping. Symbolic links are created with ln -s. As you can see below, the symbolic link gets an inode of its own.

Permissions on a symbolic link have no meaning, since the permissions of the target apply. Hard links are limited to their own partition (because they point to an inode), symbolic links can link anywhere (other file systems, even networked)

removing links

Links can be removed with rm.

The following two tabs change content below.
Ruwantha Nissanka is a Professional Cyber Security Engineer from Sri lanka with having a demonstrated history of providing cyber security services for multiple organizations in Sri Lanka. He is a positive person who wants to believe the best in others and he likes to help, encourage people and make them feel good.