Soft and Hard links in UNIX
Both symbolic and hard links are different ways to refer to a file. However, they differ in the way they point to a specific file. While a hard link refers directly to the inode of a file, symbolic links refer to a file which then refers to the inode of the file. But before moving on…what is an inode? An inode is a data structure which contains all the information regarding a specific file except for its content and name. Therefore, an inode is equal to a full address containing crucial information about the file such as the inode number, its location on the hard drive, file size, file type, owner information and metadata in general. In a way an inode is like a person’s identity card but without their name on it. It is from the inode that an operating system can pull data such as permissions and the file’s physical location within the hard drive. For instance, let’s say one file is moved to another folder, then the numerical information within its inode will change in order to reflect said move.
Ok…that’s very interesting and all but what about hard and soft links? Long story short the difference between the two is how they point to inodes. But let’s take a closer look at each one of them. A soft or symbolic link can be more easily visualised if we thought of them as shortcuts on Windows. Same as Windows shortcuts, soft links point to a file and occupy substantially less space than the files themselves. Soft links differ from the original files in many other ways such as their inode numbers, permissions and contents since the soft link only contains the path to the original file. Following the Windows shortcut idea, if the original file is deleted then the shortcuts will become useless. Likewise, if the original file is deleted then the soft links will be rendered obsolete too. Soft links are able to work between filesystems which allows them to link directories. Let’s see how we can create a soft link. Below you can see the command in order to do so,
ln -vs <source> <destination>
Now notice, that after creating it, the symbolic link is pointing to the original file but their inode numbers and permissions are completely different,
Additionally, you see that after deleting the original file if we try to display the contents of the soft link it leads nowhere,
On the other hand, hard links are basically a different name for the file. Unlike soft links, hard links have the same file size, inode number, permissions and contents as the file they point to. However, they are unable to work across different filesystems, therefore, they cannot link directories. But the most remarkable difference between the two types of links occurs when the original file is deleted. In that case the hard link will not be affected, actually it will remain as a carbon copy of the original file. Let’s exemplify all this by creating a hard link. As you can see below, the command to create a hard link is
ln -v <source> <link>
On the screenshot below you can see we created a hard link named “hard_link” to the file “example”,
And indeed you can verify that their inode numbers and permissions are the same,
Also, if we delete the original file, the hard link still remains,
That’s it! Hope you’ve enjoyed this post and that your thirst for knowledge has been quenched.