Tuesday, June 2, 2015

Hardlink and softlik difference with examples

when we create softlink the permission is different from source file while when we create hard link the permission is same as source file , why it is so ?

Below is examples of softlink and hardlink

Hardlink

[root@host-7-25 ~]# cd p2
[root@host-7-25 p2]# pwd
/root/p2
[root@host-7-25 p2]# ls -ltr
total 4
-rw-r--r--. 1 root root 0 May 18 08:59 2
-rw-r--r--. 2 root root 84 May 18 09:01 1

[root@host-7-25 p2]# ls
1 2
[root@host-7-25 p2]# ln /root/p2/1 /var/hardlink1------> command used to create hardlink

hardlink has been created

[root@host-7-25 p2]# ls -li /var/hardlink1
519 -rw-r--r--. 2 root root 84 May 18 09:01 /var/hardlink1--------> harlinked file

root@host-7-25 p2]# ls -li 1
519 -rw-r--r--. 2 root root 0 May 18 08:59 1-----------> source file

Both hardlink and source file will be having same size and same inode. even source file got deleted you will be able to access the harlinked file.

But this is not case in softlinked file. in softlink if source file is deleted you will not be able to access the soflinked file.

Below is explaination of softlinked file

[root@host-7-25 p2]# ln -s /root/p2/1 /var/softlink1---------->command used to create softlink
[root@host-7-25 p2]# ls -li /var/softlink1
459 lrwxrwxrwx. 1 root root 10 May 18 09:02 /var/softlink1 -> /root/p2/1--------> soft linked file

root@host-7-25 p2]# ls -li 1
519 -rw-r--r--. 2 root root 0 May 18 08:59 1-----------> source file

source and softlinked file inode number will be different softlinked file will be less size when compared to source file. u can access softlinked file only if source file is present. if source file is delted you will not be able to access softlinked file

Its nothing but shortcut in windows


what about different permission of soft link and source file? 

If you change the permission of harlinked file . source file permission will also change and viceversa below is example . hardlink is nothing but linking same source file to another path as backup with same size and inoder

root@host-7-57 p2]# pwd

/root/p2
[root@host-7-57 p2]# ls -ltr 1
-rwxr--r--. 2 root root 84 May 18 09:01 1---------> source file

[root@host-7-57 p2]# ls -ltr /var/hardlink1
-rwxr--r--. 2 root root 84 May 18 09:01 /var/hardlink1-----------------> hardlinked file

[root@host-7-57 p2]# chmod 555 1------> permission applied on source
[root@host-7-57 p2]# ls -ltr 1
-r-xr-xr-x. 2 root root 84 May 18 09:01 1------> source file after permission change
[root@host-7-57 p2]# ls -ltr /var/hardlink1
-r-xr-xr-x. 2 root root 84 May 18 09:01 /var/hardlink1-------------> hardlinked file will also having same permission

In soflink file you cant change the permission ---- in permission sector u can see the letter l it specifies its softlink . all soflinked file will be having same permission lrwxrwxrwx.

we can use softlink only if source file exist . But this not the case in hardlink

[root@host-7-57 p2]# ls -ltr /var/softlink1
lrwxrwxrwx. 1 root root 10 May 18 09:02 /var/softlink1 -> /root/p2/1
[root@host-7-57 p2]#

[root@host-7-57 p2]# chmod 555 /var/softlink1------------> permission apllied
[root@host-7-57 p2]# ls -ltr /var/softlink1
lrwxrwxrwx. 1 root root 10 May 18 09:02 /var/softlink1 -> /root/p2/1-------------> no change after permission apllied
[root@host-7-57 p2]#


No comments:

Post a Comment