What is the difference between a hard link and a symbolic link?
Before one can understand the difference between a hard link and a symbolic, it is necessary to understand a bit about how Unix like file systems store files. Storage on a linux system is broken into two main parts, data blocks and inodes (a number unique to each file created).
- Mode/permission (protection)
- Owner ID
- Group ID
- Size of file
- Number of hard links to the file
- Time last accessed
- Time last modified
- Time inode last modified
The data block stores actual contents of the file. Inodes point to their corresponding data block.
You may have noticed that neither the inode nor datablock stores the name of the file.
You have sharp eyes. We can better understand the purpose of a filename by understanding how directories work. You can imagine a directory as a table with two columns, the first column stores the name of the file while the second column stores the inode associated with that filename. As you can see, a filename is really just a way of referring to an inode. It would be a tremendous pain to have to remember inode numbers everytime we wanted to open a file.
So the directory contains metadata about every file that exists inside that directory, and each inode number points to it’s corresponding data block.
With that foundation (hopefully) we can start to understand the difference between hard and symbolic links. When you create a new hardlink with the command ln Target Link_name, all your doing is creating another file with the name you specify in the “link_name” field which points to the same inode as the original file. You can think of this as adding another row to your directory table where the “link_name” field goes in the filename column and the same inode as for the original file goes in the inode column.
Here is an important point. Because the new link_name points to the same inode as the original file, if you delete the original file and then run the file you created with the hardlink, you should still be able to see the same output that was in the original file.
When you create a symbolic link with the command ln -s Target Link_name, you’re creating a new file with the “link_name” field with its own inode, that points to the name of the Target file which refers to the original inode.
When you create a symbolic link, you tell the new file to point at the name of the Target file, which then points to the original inode. You can think of this adding another row to your directory table where the “link_name” field goes in the filename column and a different inode gets entered in the inode column. This means that if you delete the original filename, the symbolic link will lose access to the original inode and the data will be lost.
Hard links have an advantage over symbolic links because they take up less data in your directory table (since they reference the same inode as the original file), they don’t create anymore data blocks since they use the same inode, and they preserve data if the original file gets deleted. Symbolic links have the advantage because they can link to other directories and across entire file-systems.