Soft Link And Hard Link In Linux with Examples

Hello everyone In this Article we are going to learn what is the Soft Link And Hard Link In Linux in a very simple and easy step-by-step approach.

soft link and hard link in linux

Soft Link And Hard Link In Linux

Before Jumping in the Soft And Hard Link in Linux we should know the concept for the inode number.

Whenever we create any file and folder then the Linux kernel will generate the inode number for each file and directory.

The inode number is the unique number that is assigned to every file and directory that we create.

Let us create the file called devopstutfile.txt using the touch command. In the current location

# touch devopstutfile.txt
Screenshot 1 5

Let us create the directory called devopstutdir using the mkdir command. In the current location

# mkdir devopstutdir
Screenshot 3 3

To check the inode number for the File that we create then we can use ( -i ) option in the ls command with the file name

# ls -li devopstutfile.txt
Screenshot 4 3

To check the inode number for the directory that we create then we can use ( -i )option in the ls command with the directory name.

# ls -id devopstutdir
Screenshot 5 3

Now Please Note that we are copying the devopstut.txt file into the devopstutdir now when we check the inode number for the devoptut.txt file we can see that the inode number is changed now

# cp devopstutfile.txt devopstutdir/
Screenshot 6 2
# ls -il devopstutdir/devopstutfile.txt
Screenshot 7 3

Now Please Note that we are moving the devopstut.txt file into the devopstutdir now when we check the inode number for the devoptut.txt file we can see that the inode number is not changed.

# mv devopstutfile.txt devopstutdir/
Screenshot 8 3
# ls -il devopstutdir/devopstutfile.txt
Screenshot 9 2

This means that when we do copy operation then the inode number will not change and when we do move operation then the inode number will be changed. This means that when we do move operation then the permissions will not be changed

Links in Linux – The Links are use to create shortcut point on one or mmore locations.

Types Of Links

1 – Hard Link – The Hard Link can be created only on files, not on directories. In the hard link, the inode number is the same, If the original file deletes then there is still a backup of the file.

We have created 2 directories with the name of Directory1 and Directory2

# mkdir /Directory1 
# mkdir /Directory2

Now we are creating the file with the name as file.txt inside the Direcotry1

# touch /Directory1/file.txt
Screenshot 10 2

Now we are checking the inode number for the file.txt

# ls -il /Directory1/file.txt
Screenshot 11 2

How to can link the File to the directory – We can use the ln command to link the file to the directory. Here we are linking the file.txt to the /Directory2

# ln /Directory1/file.txt /Directory2
Screenshot 12 1

Now when we check the Data inside the Directory1 & Directory2 then we can see that the data is replicated now.

# ls /Directory1
# ls /Directory2
Screenshot 13 2

Now when we list the inode number of both the files then their inode number is same

# ls -i /Directory1/file.txt
# ls -i  /Directory2/file.txt
Screenshot 14 2

Now when we add the content on the file.txt that is inside the /Directory1 then this will also be replicated on the /Directory2 file

# echo "Welcome to devopstut.com" > /Directory1/file.txt
Screenshot 15 2
# cat /Directory2/file.txt
Screenshot 16 2

Now when we check the size of the file.txt that is inside the /Directory1 then and /Directory2 file then the size is also the same

# du -sh /Directory1/file.txt
# du -sh /Directory2/file.txt
Screenshot 17 1

Now we want to know that where the hard link of the file.txt on the whole Linux kernel then we can use the find command with the ( -inum ) option with the inode number

# find / -inum 21021385
Screenshot 18 2

Now when we check the permission of the file.txt then we can see that the file inode number is showing 2 this means that there are 2 links created of file.txt

# ls -l /Directory1/file.txt
Screenshot 19 2

Now to find where the file is located then we can first grab the inode number then we can add to the find command

# ls -i /Directory1/file.txt
Screenshot 20 1

Now if we remove the original file inside /Directory1/file.txt then still there is replication of that file is exist on the /Directory2

# rm -rf /Directory1/file.txt
Screenshot 21 1
# ls -l /Directory2/
Screenshot 1 6

The drawback of the hard link is as follow

1 – We cannot make the hard link on 2 directories if we do then this will throw the error

# ln /Directory1 /Directory2
Screenshot 2 2

2 – We cannot make the hard link when the directory is located on the different mount point for example if we create the mount point on the sda1 and different mount point on sda2 then this is not possible . Hard link is created within the same mount point only.

2 – Soft Link – The Soft Link can be created on both files and directories. In the soft link, the inode number will change, If the Original file delete then the Link has no use

We have created 2 directories with the name of Directory3 and Directory4

# mkdir /Directory3 
# mkdir /Directory4

Now we are creating the file with the name as file.txt inside the Direcotry1

# touch /Directory3/file.txt
Screenshot 3 4

Now we are checking the inode number for the file.txt

# ls -il /Directory3/file.txt
Screenshot 4 4

How to create the soft link from file to the directory – We can add the ( -s) option in the ln command to create the soft link from file to the directory

# ln -s /Directory3/file.txt /Directory4
Screenshot 5 4

Now when we check the Data with the permission inside the Directory3 & Directory4 then we can see that the shortcut is created inside the /Directory4 now.

# ls -l /Directory3
# ls -l /Directory4
Screenshot 6 3

Now when we list the inode number of both the files then their inode number is changed

# ls -i /Directory3/file.txt
# ls -i  /Directory4/file.txt
Screenshot 7 4

Now when we add the content on the file.txt that is inside the /Directory3 then this will also be replicated on the /Directory4 file

# echo "Welcome to devopstut.com" > /Directory3/file.txt
Screenshot 8 4
# cat /Directory4/file.txt
Screenshot 9 3

Now when we check the size of the file.txt that is inside the /Directory3 and /Directory4 file then the size is also changed

# du -sh /Directory3/file.txt
# du -sh /Directory4/file.txt
Screenshot 10 3

Now if we remove the original file inside /Directory3/file.txt then also the file inside the /Directory4 will also deleted

# rm -rf /Directory3/file.txt
# ls -l /Directory4
Screenshot 11 3

Now we are unlinking the file.txt from the /Directory4 using unlink command

# unlink /Directory4/file.txt
Screenshot 13 3

How to create the soft link between 2 directory – To create a soft link between directory then we can also do this, here we are creating the soft link between /Directory3 and /Directory4

# ln -s /Directory3 /Directory4
Screenshot 14 3

Now when we copy the passwd file inside the /Directory3 then the same file will be replicated to /Directory4

# cp -rf /etc/passwd /Directory3
Screenshot 15 3
# ls -l /Direcotry4/Directory3 
Screenshot 16 3

Conclusion

If you want to learn Linux in the most simple language then please click on this link.

This is the complete knowledge of HSoft Link And Hard Link In Linux if you have any doubts please feel free to comment below. Please don’t forget to join our email subscription to get the latest updates on the Linux

Leave a Comment