[Unix] Hardlinks vs Softlinks (Symlinks)

After reading numerous blog posts and some articles, here is my understanding of hardlinks and softlinks.

Both the paradigms are closely tied together by the concept of "inode" numbers. Let us first try to understand inodes.

inodes are unique numbers allocated to each file/directory on the UNIX file system. They represent the file level attributes such as permissions, blocks allocated, location etc. To view the inode number of any file/directory, simply type "ls -li".
ls -li [click on the image to enlarge]

The first list of numbers denote inode number of the respective directories.

softlink (aka symlink)
If you are from a Windows background, you would be very familier to the concept of "shortcuts". Just like shortcuts, they refer to the original files. They can point to either files or directories. Hence, both the symlink and the original file will have diffrent inode numbers. Also, as the symlink is just a shortcut, the size of the file/directory will not be same as the original file.

Once you delete the original file, the symlink stays orphan. It will not refer to any file. Any changes that you make to the contents of the file in symlink will be reflect in the original file, however the inode number and the size of the symlink will not change.

Creation of softlinks/symlinks
$ ln -s originalfilename symlinkname
softlinks/symlinks [click on the image to enlarge]
Note that in the above screenshot, the inode numbers and size of both the files are different. Also, after deletion of the original file, the symlink becomes orphan (hence the red color). Hence, although both original file are logically same, physically, they are different as they occupy different inodes and different disk blocks. symlink is therefore, just a pointer to the original file.

hardlinks are more of a UNIX only concept. As described earlier, inode is a unique number allocated to each file. The underlying difference between an symlink and a hardlink is that in case of hardlink, both the orginal file and the hardlink will have the same inode number and size.

Hence hardlinks are just "pointers" to the inodes which in turn are pointers to the data in the file.

Therefore, if you delete any one of the files, there will be no effect on the original file as the link from the filename to the inode number to the data in the file is still intact. However, any changes / amendments made to any one file will be reflected in the other file.

There is no way to determine by looking at the file as to which is the orginal file and which is the hardlink. In fact, there is no such differentiation on the UNIX file system level.

Creation of hardlinks
$ ln originalfilename hardlinkname
hardlink [click on the image to enlarge]