Soft links and Hard links in Linux

Dhilip Sanjay
4 min readSep 21, 2020

If you have ever used Linux operating system, then you must have come across the arrow symbol while executing the command ls -la :

Image showing the arrow symbol pointing to some location

If you don’t know what that means, then this article is for you.

Inodes

In Linux, inodes are the data structures which hold the information about the files. Inode number is a uniquely existing number for each file. When you create a file, a file name and inode number is assigned to it. You can view the inode number by using the command ls -i:

Displays the inode number of the files/directories

If you run out of inodes, you can't create files anymore. You can find the free inodes by running the command df -i.

Soft Links and Hard Links

Hard links are associated to the inode itself. On the other hand, Soft link points to a hard link (Note that the file name by itself is a hard link). The arrows denote that the file/directory is a symbolic link or soft link. Yep you got it right, soft links are also called symbolic links.

Soft Link vs Hard-link

Commands:

  • To create a hard link,
ln original-file hard-link
  • To create a soft link,
ln -s original-file soft-link

The ln command by default creates a hard link. With the option -s, it creates a soft link.

Creating hard link and soft link

Pros and Cons

1) Renaming the original file

  • When the original file is renamed, soft link will be pointing to non-existing file. But hard link can access the file because it is linked with the inode itself. So renaming the original file doesn’t affect the hard link.
Renaming the original file

2) Deleting the original file

  • When the original file is deleted, the soft link will be pointing to non-existing file or directory. Even if the original file is deleted, the hard link will be still pointing to the inode.
  • To change the already existing soft link, you can use -f option (force) of the ln command:
ln -sf renamed-file soft-link

Only after all the hard links are deleted, the inode will be deleted.

Changing the soft-link to point to renamed file & Deleting original file

3) Soft Link Inode vs Hard Link Inode

  • Hard link will have the same inode as the original file.
  • Soft link will have its own inode.

As mentioned earlier, ls command with -i option displays the inodes.

Hard link and original file having the same inode & Soft link has its own inode.

4) Across File systems

  • Hard link — cannot link a file across file systems.
  • Soft link — can link a file across file systems.

To view the file system and mount points : df -hT

Option -T (Print file system type)

Hard links cannot be created across file systems, whereas soft links can be created.

It is important to note the permissions on the symlink. It has full 777 permissions, meaning that in theory you can execute the symlink, however since it is just a reference, in reality it has the same permissions as the original file.

“Don’t try to learn leaves without learning the roots!”

Hope you learnt something new today! If so, then why haven’t you clapped yet ?! Go ahead and give the claps!

--

--

Dhilip Sanjay

Aspiring Bug Bounty Hunter | CyberSecurity | WebApp Security | Network Security Enthusiast