Friday, September 25, 2015

Hard Link vs Soft Link



What is Hard Link ?

                Hard Link is a mirror copy of the original file. Hard links share the same inode.Any changes made to the original or Hard linked file will reflect the other.Even if you delete any one of the files, nothing will happen to the other.

What is Symbolic Link ?

                Soft Link or symlink  is a symbolic link to the original file. Soft Links will have a different Inode value.A soft link points to the original file. If you delete the original file, the soft link fails. If you delete the soft link, nothing will happen. symlink is kind of windows shortcut.

Difference between Hard Link and Soft Link in Linux

Example: Create a file " original-file.txt "




Now lets create a Hard Link
Example: HardLink-file.txt



Now lets create a Soft Link
Example: SoftLink-file.txt




From the above ls -il result, you find the same inode for " HardLink-file.txt " and " original-file.txt ".
Inode value is different for the soft link " SoftLink-file.txt ".

Now lets try editing the original file:
Example:


  You can see after editing the original file the contents are reflected to all the links(hard and soft).

Now lets try changing the permissions:




From the above example its clear that changing the permission of " original-file.txt " will update the permission set of " HardLink-file.txt ".

The soft link remains unchanged. 



Now lets try deleting the original file.



So removing the original file will affect the Soft link. The Soft link fails.
Hard link is unaffected.