Sep 30, 2008

Difference between Soft and Hard links

Soft Link:
1. Soft links are links to a file but not the inode.
2. To create: ln -s file1 file2
3. To view: ls -li
131135 lrwxrwxrwx 1 user user 5 Jul 10 09:04 file2 -> file1
131137 -rw-r--r-- 1 user user 35 Jul 10 09:03 file1
4. The inode for file1 is 131137 and inode for file2 is 131135.
5. If you see the permission bits, there is 'l' in the front for a soft link.
6. If file1 is deleted, the link still exists. But if you try to view file2, its empty. This means that once the main file is deleted the data is gone.

Hard Link:
1. Hard links are links to inode
2. To create: ln file1 file2
3. To view: ls -li
131136 -rw-r--r-- 2 user user 48 Jul 10 09:27 file1
131136 -rw-r--r-- 2 user user 48 Jul 10 09:27 file2

4. The inode for file1 and file2 is the same (131136).
5. If you see the output above for "ls -i", file2 does not show that it is linked to file1. In reality it is not linked to file1 but it is linked to the inode.
6. If you see that there is number '2' before the username 'user'. This shows the number of hard links to the inode.
7. If file1 is deleted, the data is not deleted. If you view file2 the data is still there. Deleting file1 only deletes a link. The data is gone once the last hard link is deleted.

No comments: