Hard Link and Symbolic Link??

Before getting into the term Symbolic link and Hard link ,lets understand the term ‘inode’ . A Unix file is “stored” in two different parts of the disk — the data blocks and the inodes. The data blocks contain the “contents” of the file. But the information about the file is stored elsewhere — in the inode. Basically, inode is a file structure on a file system. More easily, it is a “database” of all file information except the file contents and the file name.Both the inodes and data blocks are stored in a “filesystem” which is how a disk partition is organized.

Coming to Symbolic Links and Hard Links..

Symbolic link (Symlinks/Soft links) are links between files. It is nothing but a shortcut of a file(in windows terms).

  • You can delete the soft links without affecting the actual file or directory it is pointing to. The reason is because the inode of the linked file is different from that of the inode of the symbolic link. But if you delete the source file of the symlink ,symlink of that file no longer works or it becomes “dangling link” which points to nonexistent file .
  • Soft link can span across filesystem.
  • Soft links can link both files and directories.
  • A symbolic link shows the character l (file type corresponding to symbolic link) before the permissions for user, group and other users and displays an arrow followed by another file name, meaning it’s a link to another file (see image below).

To create Symlink ,

ln -s <source> <linkname>

Hard link is the exact replica of the actual file it is pointing to .Both the hard link and the linked file shares the same inode .

  • If the source file is deleted ,the hard link still works and you will be able to access the file until the number of hard links to file isn’t 0(zero).
  • Hard links cannot span across filesystem . Why? Hard links are sharing the same inode — they point to the hardware location, instead of directory tree. This means that if you were to try to make hard link between ext3 and ext4, then you’d have some problems — these filesystems are different, so they can’t share the inode.
  • Hard links can link only files,not directories. The reason hard-linking directories is not allowed is a little technical. Essentially, they break the file system structure.
  • If the source file of hardline is removed ,the link still works. With hard links, there is no concept of original file and links , both files are equal.
  • Hard links are especially useful in performing “copy on write” applications. They allow you to keep a backup copy of a directory structure, while only using space for the files that change between two versions.
  • A hard link appears as an ordinary file (meaning you won’t see an l, but a “-” that stands for ordinary file, and no arrow will be displayed).

To create Hard link,

ln <source> <linkname>

So in conclusion, both are pointers to a file. The difference is the kind of pointer. Symlink points to another file by name whereas Hard link points by inode number.