Sunday, 7 June 2015

Soft Links and Hard Links in Linux

Soft link (symbolic link / symlink):

It is similar to “shortcuts” in windows operating system. Removing the original file does not remove the attached symbolic link or symlink, but without the original file, the symlink is useless.
Command to create soft link is
#ln –s <target file> <new file (name of symlink)>

symlink is simply contains the pointer to the location of the destination file. In more technical words, in soft link, a new file is created with a new inode, which have the pointer to the inode location of the original file.

Links to directory: If you want to link directories, then you must be using Soft links, as you can’t create a hard link to a directory.
Link across filesystems: If you want to link files across the filesystems, you can only use symlinks/soft links.

This is a  special file type whose data part carries a path to another file. 

Hard link:

In technical words, only an entry into directory structure is created for the file with its inode number, but it points to the inode location of the original file. This means there is no new inode creation in the hard link. Inode of file maintains count of links pointing to this file, when count becomes zero, inode is free to de-allocate. 

Hard link can only refer to data that exists on the same file system.
Example :  '..' is a hardlink to the file that implements the immediate parent of the current directory.

  • If you move the source file to some other location on the same filesystem, the hard link will still work, but soft link will fail. 
  • Hard links takes very small amount of space, as there are no new inodes created while creating hard links.
  • If you want to make sure safety of your data, you should be using hard link, as in hard link, the data is safe, until all the links to the files are deleted, instead of that in soft link, you will lose the data if the master instance of the file is deleted.

Command to create hard link is 
#ln <target file> <hardlink name>

If you try #ls -il  , you will see that both files (link and source file) have same inode number ,  files have the same file permissions and the same size. Because that size is reported for the same inode, we can see that a hard link does not occupy any extra space on your space.

No comments:

Post a Comment