Thursday 3 November 2016

Soft vs Hard Link in Linux

In linux we have something called links which replicates contents of one file to another with symbolic links. We called them -
      a. Soft Link
      b. Hard Link
Below is a small diagram which explains the full story.



Soft Link

Soft link is symbolic link of one file or directory. We create soft link using the command -
shell> ln -s file_to_be_linked linked_file 

For example, we need to link postgresql-9.3.1 to postgresql. We will issue the below command -
shell> ln -s postgresql-9.3.1 postgresql

In soft link, if we delete/rename the postgresql, the contents are deleted permanently. But if we replace the contents of postgresql-9.3.1, there will be no harm to the link. But if we rename postgresql-9.3.1, then postgresql wont work because soft link was linked with file not the content. If we move postgresql-9.3.1 to different location, postgresql will not be accessible.

We create such links where we want the file name to be present in the directory but content should append in different location.

Hard Link

Hard link are links which contents same information between two files. We create hard link issuing the below command -
shell> ln postgresql-9.3.1 postgresql

If we delete postgresql-9.3.1, still we will be able to access postgresql because the link file points to the same inode. Inode is a data structure which keeps information of all files like size, user id, group id, permission. Hardlinks can only be possible within the same file system as inode is same. Hard links are only possible for files not for directory. Performance will be better here as it will contact the disk pointer.


No comments:

Post a Comment