You are here
Home > Redhat > Difference between Hard link and Soft link

Difference between Hard link and Soft link

What is the difference between Hard Link and Soft Link in Linux?

Hard Link is a mirror copy of the original file. Hard links share the same inode.

Any changes made to the original or Hard linked file will reflect the other.

Even if you delete any one of the files, nothing will happen to the other.

Hard links can't cross file systems.

Soft Link is a symbolic link to the original file. Soft Links will have a different Inode value.

A soft link points to the original file. If you delete the original file, the soft link fails. If you delete the soft link, nothing will happen.

Hard links can cross file systems.
 

Difference between Hard Link and Soft Link in Linux
 

Example: Create a file " original-file.txt "

smart:~ #

smart:~ # echo "Learning about Hard and Soft Link" > original-file.txt

smart:~ # cat original-file.txt

Learning about Hard and Soft Link
 

Now lets create a Hard Link
 

smart:~ # ln original-file.txt HardLink-file.txt

smart:~ # ls -il

total 8

840388 -rw-r–r– 2 smartentry smartentry 33 2009-05-18 09:16 HardLink-file.txt

840388 -rw-r–r– 2 smartentry smartentry 33 2009-05-18 09:16 original-file.txt

 

Now lets create a Soft Link
 

Example: SoftLink-file.txt

 

smart:~ # ln -s original-file.txt SoftLink-file.txt

smart:~ # ls -il

total 8

840388 -rw-r–r– 2 smartentry smartentry 33 2009-05-18 09:16 HardLink-file.txt

840388 -rw-r–r– 2 smartentry smartentry 33 2009-05-18 09:16 original-file.txt

840186 lrwxrwxrwx 1 smartentry smartentry 17 2009-05-18 09:23 SoftLink-file.txt -> original-file.txt

 

From the above ls -il result, you find the same inode for " HardLink-file.txt " and " original-file.txt ".

Inode value is different for the soft link " SoftLink-file.txt ".

 

Now lets try editing the original file:

 

Example:

smart:~ # cat >> original-file.txt

Editing this file to learn more!!

 

smart:~ # cat original-file.txt

Learning about Hard and Soft Link

Editing this file to learn more!!

 

smart:~ # cat HardLink-file.txt

Learning about Hard and Soft Link

Editing this file to learn more!!

 

smart:~ # cat SoftLink-file.txt

Learning about Hard and Soft Link

Editing this file to learn more!!

 

Now lets try changing the permissions:

Example:

smart:~ # chmod 700 original-file.txt

smart:~ # ls -il

total 8

840388 -rwx—— 2 smartentry smartentry 67 2009-05-18 09:34 HardLink-file.txt

840388 -rwx—— 2 smartentry smartentry 67 2009-05-18 09:34 original-file.txt

840186 lrwxrwxrwx 1 smartentry smartentry 17 2009-05-18 09:23 SoftLink-file.txt -> original-file.txt

 

From the above example its clear that changing the permission of " original-file.txt " will update the permission set of " HardLink-file.txt ".

The soft link remains unchanged.

Now lets try deleting the original file.

 

smart:~ # rm original-file.txt

smart:~ # ls -il

total 4

840388 -rwx—— 1 smartentry smartentry 67 2009-05-18 09:34 HardLink-file.txt

840186 lrwxrwxrwx 1 smartentry smartentry 17 2009-05-18 09:23 SoftLink-file.txt -> original-file.txt

 

smart:~ # cat HardLink-file.txt

Learning about Hard and Soft Link

Editing this file to learn more!!

smart:~ # cat SoftLink-file.txt

cat: SoftLink-file.txt: No such file or directory

So removing the original file will affect the Soft link. The Soft link fails.

Hard link is unaffected.

Leave a Reply

Top