Thursday, October 8, 2015

Soft Link Versus Hard Link in Unix

We all work on unix for long time. But many of us may not know the exact difference between soft link(symlink) and Hard Link. Today i am going to cover how to create and what is the difference between two.

Soft Link(Symlink) Soft link is like shortcut in windows. It is pointer to another file. Soft Link creates new file with new inode  which has the pointer to the inode location of the original file. 

The below diagram explain how soft link works.

















Here is an example to create soft link.







The soft link will be invalid if we delete or move the original file to different location. 









Soft link can also be pointer to different file system or directory.

















Hard Link Hard Link is bit different than the soft link. It reference the inode directly on the disk. The hard link will not break if we move the original files to different location or delete the original file.  We can create the hardlink only for files(not directories) . 

The below diagram explain how the hard link works!





















Here is an example to create Hard link.











The hard link never be broken when we delete or move the original file to different location.














Hard Link can not be created across the file system.  but soft link does!







Hard Link can not be created for directories! but soft link does!









Now we have better idea how both links works.

Conclusion :


When to use Soft link?


  1. To create a link across the file system
  2. To create the link for directories


When to use Hard link?

  1. In case if you want to move the file to some other location on the same file system
  2. It does not create new file with different inode. Hence it takes less storage. 
  3. Performance wise it is slightly better. since it is direct access

No comments: