Tuesday, March 31, 2015

Hard link and Softlink in linux with examples

Link are of two types Soft links (symbolic links ) or Hard link
Soft Links (symbolic links )
You can make links for files & folder & you can create link (shortcut) on different partition & got different inode number from original.
If real copy is deleted the link will not work.
Hard Links
For files only & you cannot create on different partition ( it should be on same partition ) & got same inode number as original
If the real copy is deleted the link will work ( because it act as original file )


[root@client1 ~]# cd /myfolder 
[root@client1 myfolder]# ls 
[root@client1 myfolder]# cat > original_file 
This is the original file to test links 
[root@client1 myfolder]# ls -ld original_file 
-rw-r--r-- 1 root root 40 Jul 12 21:12 original_file [root@client1 myfolder]# cd 
Setting a soft link 
[root@client1 ~]# ln -s /myfolder/original_file softlink [root@client1 ~]# ls -ld softlink 
lrwxrwxrwx 1 root root 23 Jul 12 21:12 softlink -> /myfolder/original_file 

[root@client1 ~]# cat softlink 
This is the original file to test links 
[root@client1 ~]# cat >> softlink 
this line is appended by the softlink 
[root@client1 ~]# cat /myfolder/original_file 
This is the original file to test links 
this line is appended by the softlink 
[root@client1 ~]# ls -ld /myfolder/original_file 
-rw-r--r-- 1 root root 78 Jul 12 21:13 /myfolder/original_file 

[root@client1 ~]# ls -ld softlink 
lrwxrwxrwx 1 root root 23 Jul 12 21:12 softlink -> /myfolder/original_file 
[root@client1 ~]# rm /myfolder/original_file 
rm: remove regular file `/myfolder/original_file'? y [root@client1 ~]# cat softlink 
cat: softlink: No such file or directory 

Setting a Hard Link 

[root@client1 ~]# ln /myfolder/original_file hardlink 
ln: creating hard link `hardlink' to `/myfolder/original_file': Invalid cross-device link 
[root@client1 ~]# cd / 
[root@client1 /]# cat > original 
This is the original file to test links
[root@client1 /]# cd 
[root@client1 ~]# ln /original /root/hardlink [root@client1 ~]# ls -ld /root/hardlink 
-rw-r--r-- 2 root root 41 Jul 12 21:16 /root/hardlink [root@client1 ~]# ls -ld /original 
-rw-r--r-- 2 root root 41 Jul 12 21:16 /original [root@client1 ~]# cd / 
[root@client1 /]# cat > original 
This is the original file to test links. 
[root@client1 /]# cd 
[root@client1 ~]# ln /original /root/hardlink [root@client1 ~]# ls -ld /root/hardlink 
-rw-r--r-- 2 root root 41 Jul 12 21:16 /root/hardlink 
[root@client1 ~]# ls -ld /original 
-rw-r--r-- 2 root root 41 Jul 12 21:16 /original [root@client1 ~]# cat >> hardlink 
this is appended by the hardlink file 
[root@client1 ~]# ls -ld /root/hardlink 
-rw-r--r-- 2 root root 79 Jul 12 21:18 /root/hardlink [root@client1 ~]# ls -ld /original 
-rw-r--r-- 2 root root 79 Jul 12 21:18 /original [root@client1 ~]# rm /original 
rm: remove regular file `/original'? y
 [root@client1 ~]# cat hardlink 
This is the original file to test links. 
this is appended by the hardlink file 



















No comments:

Post a Comment