A Little About Linux: Hard and Symbolic Links

Links are the Linux equivalent of aliases or shortcuts on other platforms.  I'm not going to go into why you would use them as most people who are reading this will be familiar with the concept, even if it's on another Computer platform.  The difference comes between the types of Links, which are Soft/Symbolic and Hard.

Symbolic Links
These links (symlinks) are the easiest of the two forms to understand.  It's the same as a Windows shortcut.  Essentially it's an alias to an actual file, but it contains no actual information about that file it just points to the file on the disk.  Behind the scenes, when a symlink is created, it actually creates a separate inode on the disk which points to the actual inode of the target file.

  • If you delete the symlink, the file it points to (target) is NOT deleted.  
  • If you move the target file to somewhere else, the symlink will not work.  
  • If you delete the target file, the symlink will not work.
  • Symlinks will point to any file or directory, regardless on what volume the target resides.

Hard Links
This is the default type of link in Linux.  Although superficially it looks the same as a symlink, in that an alias is created that points to the target file, behind the scenes it points directly to the inode of the target file on disk.  Due to this direct linking, it means the exact number of hard links to the target file can be calculated. 

The command that is used for creating Links is ln.  The format is;

ln [OPTION]   TARGET   LINK_NAME 

For creating a Symbolic Link;
ln -s /usr/bin/vim vimmy

For creating a Hard Link;
ln /usr/bin/vim vimmy

No comments: