Before understanding the difference between soft link and hard link, we need to understand the concept of soft link and hard
link as well as concept of Inode in unix
file system. Actually, link concept originated to replicate the file at
different location without copying it and it is based on file system. File
System is closely related withinode which is unique in system.
How to
create Soft Link in Linux?
We will create soft
link as well as hard link in two examples and then we will check for
difference.
root@hello:~/ctier/shellscript#
pwd
/root/hello/shellscript
root@hello:~/ctier/shellscript#
ls -li shellscript.sh
5644164
-rwxr-xr-x 1 root root 446 2013-04-17 17:48 shellscript.sh
root@hello:~/ctier/shellscript#
ln -s /root/ctier/shellscript/shellscript.sh
/root/ctier/test/shells.sh
root@hello:~/ctier/shellscript#
ls -li /root/ctier/test/shells.sh
6554654
lrwxrwxrwx 1 root root 16 2013-04-24 22:10 /root/ctier/test/shells.sh -> /root/hello/shellscript/shellscript.sh
root@hello:~/ctier/shellscript#
|
As per above example, we have created soft link of shellscript.shto
this file /root/ctier/test/shells.sh. i.e. new file is /root/ctier/test/shells.sh.
If you check inode of new file, it is different. And in long listing, it is
showing that it points to some other file. Size of file is different at both
the locations. Because in soft link, there is no copy of file, it is just
pointing to the original location. If we are trying to open a /root/ctier/test/shells.sh
this file, then it will open original file i.e. /root/ctier/shellscript/shellscript.sh.
If we made any changes in original file then it will reflect in soft linked
file as well.
How to
create Hard Link in Linux?
root@hello:~/hello/shellscript#
ls -li shellscript.sh
5644164
-rwxr-xr-x 1 root root 446 2013-04-17 17:48 shellscript.sh
root@hello:~/hello/shellscript#
mkdir test
root@hello:~/hello/shellscript#
ln shellscript.sh ./test/hardlink.sh
root@hello:~/hello/shellscript#
ls -li ./test/hardlink.sh
5644164
-rwxr-xr-x 2 root root 446 2013-04-17 17:48 ./test/hardlink.sh
root@hello:~/hello/shellscript#
|
If you see above example of hardlink then you can understand thatin hard link, file’s new copy
is created at new location with its original size. Original file’s changes will
reflect in new file. But there is no linking showing in long listing because
new file is pointing to the original file’s inode. if we delete any one of file
or original file then it will not impact on this hard link. Another file will
be accessible.
Difference
between Soft Link and Hard Link
Difference
|
Soft Link
|
Hard Link
|
Inode
|
Inode will be different for both the files
|
Inode will be same for both the files
|
Deletion of original file
|
Deletion of original file, impact on link. Another file will not be
accessible.
|
Deletion of original file, no impact on link. Another file will be
accessible.
|
Time to execute
|
Access time is slow as compared to hard link
|
Access time is fast as compared to soft link
|
Cross File System
|
In Cross file system, Soft link works
|
In cross file system, hard link is not working. Needed same file
system
|