Friday, April 26, 2013

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:~/ctier/shellscript# ls -li
5644164 -rwxr-xr-x 1 root root 446 2013-04-17 17:48
root@hello:~/ctier/shellscript# ln -s /root/ctier/shellscript/ /root/ctier/test/
root@hello:~/ctier/shellscript# ls -li /root/ctier/test/
6554654 lrwxrwxrwx 1 root root 16 2013-04-24 22:10 /root/ctier/test/ -> /root/hello/shellscript/

As per above example, we have created soft link of shellscript.shto this file /root/ctier/test/ i.e. new file is /root/ctier/test/ 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/ this file, then it will open original file i.e. /root/ctier/shellscript/ 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
5644164 -rwxr-xr-x 1 root root 446 2013-04-17 17:48
root@hello:~/hello/shellscript# mkdir test
root@hello:~/hello/shellscript# ln ./test/
root@hello:~/hello/shellscript# ls -li ./test/
5644164 -rwxr-xr-x 2 root root 446 2013-04-17 17:48 ./test/

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

Soft Link
Hard Link
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

Posted by Machindra Dharmadhikari On 4/26/2013 10:03:00 AM No comments


Post a Comment

  • RSS
  • Delicious
  • Digg
  • Facebook
  • Twitter
  • Linkedin
  • Youtube

    Chitika Ads 2



    Enter your email address:

    Delivered by FeedBurner