Saturday 19 January 2013

Difference between Hard Link and Soft Link


Difference Between Hard Link and Soft Link:-


HARD LINK:-
 Hard link is acts like a mirror copy of the original file. These links share the same inodes.
Changes made to the original or hard linked file will reflect the other. When you delete hard link nothing will happen to the other. Hard links can’t cross file systems.

Features:-
1)      Have same inodes number.
2)      Ls –l command shows all the links with the link column shows  number of links.
3)      Links have actual file contents.
4)      Removing any link, just reduces the link count, but doesn’t affect other links.
5)      You cannot create a hard link for a directory.
6)      If original file is removed then the link will still show the content of the file.

Example:-
HardLink:-
[root@localhost ]# mkdir test
[root@localhost ]# cd test/
[root@localhost test]# ls
[root@localhost test]# touch test1
[root@localhost test]# ln test1  test2
[root@localhost test]# ls -il  test1  test2
1335106 -rw-r--r--. 2 root root 0 Jan 19 16:29 test1
1335106 -rw-r--r--. 2 root root 0 Jan 19 16:29 test2
[root@localhost test]#
From the output you can noticed that both file has same inode number and Size also same.
Now remove test1 and check the content of test2 file.
[root@localhost test]# rm  test 1
rm: remove regular empty file `test1'? y
[root@localhost test]# cat test2
You will still be able to same content in test2.
………………………………………………………………………………………………………………………………………………………………
Soft Link:-
Soft Link is actual link to the original file. These Links will have a different inodes Value.
Softlink points to the original file so if original file is deleted the soft link fails. If you delete
The soft link, nothing will happen to file. The reason for this is, the actual file or directory’s
Inode is different from the “Soft Link” created file’s inode,Hard links can cross file System.

Features:-
1)      Soft links have different inodes number.
2)      Ls –l command shows all link with second column value 1 and the link points to original file.
3)      Soft Link contains the path for original file and not the contents.
4)      Removing soft link doesn’t affect anything but removing but removing original file, the link points to the nonexistent file.
5)      A soft link can link to a directory.

Example:-
[root@localhost test]# ln -s  test2  test3
[root@localhost test]# ls -li  test2   test3
1335106 -rw-r--r--. 1 root root 0 Jan 19 16:29  test2
1336783 lrwxrwxrwx. 1 root root 7 Jan 19 16:39 test3 -> test2
[root@localhost test]#
From the output you can notice that the both file have different inode number and Permissions.
[root@localhost test]# cat  test2
[root@localhost test]# cat test3
Now remove the original file:
[root@localhost test]# rm  test2
rm: remove regular empty file `test2'? y
[root@localhost test]# ls
test3
It will display Symbolic link  test3 but if you try to list the contents  it will tell you that there is no such file getting error message.
[root@localhost test]# cat  test3
cat: test3: No such file or directory
[root@localhost test]# ls

No comments:

Post a Comment