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
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
Let us create the directory called devopstutdir using the mkdir command. In the current location
# mkdir devopstutdir
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
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
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/
# ls -il devopstutdir/devopstutfile.txt
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/
# ls -il devopstutdir/devopstutfile.txt
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
Now we are checking the inode number for the file.txt
# ls -il /Directory1/file.txt
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
Now when we check the Data inside the Directory1 & Directory2 then we can see that the data is replicated now.
# ls /Directory1
# ls /Directory2
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
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
# cat /Directory2/file.txt
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
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
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
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
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
# ls -l /Directory2/
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
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
Now we are checking the inode number for the file.txt
# ls -il /Directory3/file.txt
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
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
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
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
# cat /Directory4/file.txt
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
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
Now we are unlinking the file.txt from the /Directory4 using unlink command
# unlink /Directory4/file.txt
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
Now when we copy the passwd file inside the /Directory3 then the same file will be replicated to /Directory4
# cp -rf /etc/passwd /Directory3
# ls -l /Direcotry4/Directory3
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