Monday, December 12, 2011

Hard Link Vs Soft Link in Linux

A Link is a connection of one file with other.

Links allow more than one file name to refer to the same file, elsewhere. In Linux we make the use of ln command to create links of a file.

There are two types of links, both of which are created by ln:
  • symbolic links, which refer to a symbolic path indicating the abstract location of another file, and
  • hard links, which refer to the specific location of physical data.
These links behave differently when the source of the link (what is being linked to) is moved or removed. Symbolic links are not updated (they merely contain a string which is the pathname of its target); hard links always refer to the source, even if moved or removed.

For  symbolic links we make use of ln -s and for hard links we make use of ln command as shown below :
1.) symbolic links : 
[root@server199 ks]# ln -s sed.sh softlink.sh 
[root@server199 ks]# ll -rs
total 8
0 lrwxrwxrwx 1 root root   6 Dec 12 18:03 softlink.sh -> sed.sh
4 -rw-r--r-- 2 root root 107 Dec 11 18:34 sed.sh
4 -rw-r--r-- 2 root root 107 Dec 11 18:34 hardlink.sh
 
2.) hard links : 
[root@server199 ks]# ll
total 4
-rw-r--r-- 1 root root 107 Dec 11 18:34 sed.sh
[root@server199 ks]# ln sed.sh hardlink.sh 
[root@server199 ks]# ll
total 8
-rw-r--r-- 2 root root 107 Dec 11 18:34 hardlink.sh
-rw-r--r-- 2 root root 107 Dec 11 18:34 sed.sh

Here I am not going in deep just wonna to share difference between these two.
1.) Hard Link create a link file having same contents as in original file like symbolic Link does. But In Hark link both files have same i-node number.
e.g.
[root@server199 ks]# ll -i
total 8
7897090 -rw-r--r-- 2 root root 107 Dec 11 18:34 hardlink.sh
7897090 -rw-r--r-- 2 root root 107 Dec 11 18:34 sed.sh
[root@server199 ks]# ll -i
total 8
7897090 -rw-r--r-- 2 root root 107 Dec 11 18:34 sed.sh
7897091 lrwxrwxrwx 1 root root   6 Dec 12 18:03 softlink.sh -> sed.sh

2.) Hard Link Can not be created on Directories, But Symbolic Link can be.
e.g.
[root@server199 ks]# ln test/ dirhardlink
ln: `test/': hard link not allowed for directory
[root@server199 ks]# ln -s test/ dirsoftlink
[root@server199 ks]# ll
total 12
lrwxrwxrwx 1 root root    5 Dec 12 18:10 dirsoftlink -> test/
drwxr-xr-x 2 root root 4096 Dec 12 18:08 test
 3.) If you delete original file then through Hard Link you can still access the contents of file but not applicable in case of Soft Links.
e.g.
[root@server199 ks]# ll -i
total 8
7897090 -rw-r--r-- 2 root root 107 Dec 11 18:34 hardlink.sh
7897090 -rw-r--r-- 2 root root 107 Dec 11 18:34 sed.sh
7897091 lrwxrwxrwx 1 root root   6 Dec 12 18:15 softlink.sh -> sed.sh

[root@server199 ks]# rm -rf sed.sh

[root@server199 ks]# cat hardlink.sh
 HI This is testing File for testing working function of sed for converting small letters to capital ones.

[root@server199 ks]# cat softlink.sh
cat: softlink.sh: No such file or directory

[root@server199 ks]# ll
total 4
-rw-r--r-- 1 root root 107 Dec 11 18:34 hardlink.sh
lrwxrwxrwx 1 root root   6 Dec 12 18:15 softlink.sh -> sed.sh
[root@server199 ks]#

If you have some more then please share.

!Enjoy Linux
Kuldeep Sharma

No comments:

Post a Comment

Kubernetes 1.31 || Testing the Image Volume mount feature using Minikube

With Kubernetes new version 1.31 ( https://kubernetes.io/blog/2024/08/13/kubernetes-v1-31-release/ ) there are so many features releases for...