Under standing Hard link and soft link in Linux

 

Explaining Soft-link and Hard-link in Linux?

A soft-link or symbolic link is an actual link to the original file,Whereas a hard-link is a mirror copy of the original file.If you delete the original file,the soft-link has no value,because it points to a non-existent file.But in the case of hard-link,it is entirely opposite.

Even If you delete the original file,the hard-link can still has the data of the original file. Because hard-link acts as a mirror copy of the original file.

Let us see some of the difference between softlink and hardlink

Hard-link:

It refers to the specific location of physical data.

  1. Any changes made to the original or hard-linked file will reflect the other
  2. Even if you delete anyone of the files,nothing will happen to the other hard-links
  3. You cant link a directory even within the same file system
  4. In hard-link method both original file and hard-linked file will have the same inode number.

Soft-link

  1. Soft-link is a symbolic link to the original file(like windows shortcut)
  2. Soft-links will have different inode numbers
  3. Soft-link points to the original file.If you delete the original file,the soft-link fails.
  4. You can link a directory using soft-link on same file system and also on other file system.

How to create a Soft-Link file?

Let us create an empty directory with the name  “backup”

#mkdir  /backup

Change your path to the /backup directory

#cd   /backup

cd (change directory)-Command used to change your path

#pwd

sample output:/backup

pwd(Present working directory)- Used to check your current directory path

Now,Create a file with the name  “sourcefile” with some data as shown below

#cat  > sourcefile

Welcome to Vasanth blog

Now,let us view the data of the sourcefile

#cat  sourcefile

sample output:Welcome to Vasanth blog

Now,Create a symbolic link to the sourcefile

Syntax:

#ln   -s   <sourcefile>   <linkfile>

s—>Soft-link

#ln  -s  sourcefile   mysoftlink

 

Now,Let us view the data of the  mysoftlink file

#cat   mysoftlink

Sample output: Welcome to Vasanth blog

As you can see in the above output,”mysoftlink” file displays the same data as “sourcefile”

Now,Check the inodes and permissions of mysoftlink and sourcefile

As we can see in the above screen shot,Even though the mysoftlink file has the same contents as sourcefile,the inode numbers (1177444 vs 1177349) are different and the file permissions are different .

Hence it is proved that the soft-link doesn’t share the same inode number and permission of original file

Now let us remove the original file(i.e sourcefile) and see what happens

#rm  -vf  sourcefile

Sample output:removed "sourcefile"

Check  whether you can  access the mysoftlinkfile

#cat   mysoftlink

Sample output:mysoftlink:No such file or directory

As you see above,there is no such file or directory called mysoftlink after we removed the original file(i.e sourcefile).so,now  we understand that soft-link is just a link that points to the original file

Note:However,if you remove the link-file,the original file will still present.

 

Creating Hard Link:

Create a file “myfile.txt”  with some contents as shown below:

#echo “Welcome to Vasanth Blog” >/myfile.txt

or

#cat  >/myfile.txt

Let us verify the contents of the file

#cat   /myfile.txt

Sample output:Welcome to Vasanth Blog

The source file with the name myfile.txt has created now

Let us create the hard-link to the source file “myfile.txt” by using the following command

Syntax:

#ln  <source file>  <link file>

So “ln” command without option will create the hard-link

#ln  /myfile.txt  /myhardlink

Check the contents of the linkfile

#cat  /myhardlink

Now,Let us check the inode number for both the files,it should display different inode number for the source file and the link file in hard link.

#ls   -i   /myfile.txt

#ls  -i   /myhardlink

 

Now,Check the permission for the sourcefile and the  link file

#ls   -lit   /myfile.txt

Here the option “-i” to display the inode number and “-t”  from the output to display the latest modified file from the directory in the first.

 

Now,we see both the hard-link and the source file are having the same inode number(15920) and file permission(-rw-r–r–).

Hence it is proved that the hard-link file shares the same inode number and permission of the original file

Now,remove the  source file and see what happens

#rm    -vf   /myfile.txt

Sample output:removed `myfile.txt`

After removing the source file check the content of the hard-link file

#cat    /myhardlink

Sample output:Welcome to Vasanth Blog

As you  can see from the above screen shot output,even though if i delete the original file i can still view the contents of the hard-link file

Hence it has been proved that the hard-link  shares the same inode number,permissions and data of the original file.

 

#############################################################################################

You may have a question pop up in your mind in soft-link,  once you delete the original file how the link file gets removed .

here you go…..

Let me first explain the hard-link in detail:The file references or points to a spot on a hard-drive ,it is a representation of hard-drive, exactly the inode layer on the hard-drive how the hard-disk stores the data.

So myfile.txt points to a specific spot on a hard-drive where its store data,when you create hard-link file myhardlink it pointing to the same exact spot on the hard-disk. So it effectively gives you is two separate files with the exception if you edit the myfile.txt myhardlink file will also get edited because its pointing to same spot on the hard disk .

Soft-link: It works in a different way ,now the source file(i.e original file) in our case does the same thing,points to the spot on the hard-drive to stores its data.

When you create soft-link, instead of another file (i.e link file)pointing to the same spot on the hard-drive ,this file actually points to the sourcefile(i.e original file),It points to the name of the file instead of pointing to spot on the hard drive.

So If you delete the original file then the link file(in our case mysoftlink)will be completely useless because it is going to pointing at something that doesn’t exists.

If you found this article useful, Kindly Subscribe here
👉🏿👉🏿 Click this link to Subscribe

 

 

 

About Author:

Hello readers! Let me introduce my self first. My name is Vasanth Nirmal Singh J S having 9+ years of experience in IT on all flavours of Unix operating systems ,Storage's and many more .. I would like to share my technical experience i have come across - can be help to other people. So in this blog, I'll post my thoughts related to ITIS. I'll share experiences that I've had while working in different environments. You can expect content related to Unix,Solaris,Linux,EMC Storeages,HP-UX and many others. I hope this blog can be useful for you! Your comments will be appreciated!

2 thoughts on “Under standing Hard link and soft link in Linux

Leave a Reply

Your email address will not be published. Required fields are marked *