Explain Soft Link and Hard Link in linux with practical example


Explain Soft Link and Hard Link in linux with practical example

In this tutorial we learn what is Soft Link and what is Hard Link and their feature with practical explanation. First we need to know the definition of Soft Link and Hard Link in Linux Operating system.

What is Soft Link and its feature in Linux

Soft Link is a Symbolic Link of Linux Operating system. Soft Link is an original Link of the main file. If we remove the main file from the system there is no meaning of the Soft Link.
Feature :

  •        Soft Link can cross the file system.
  •        Soft Link has different inode no from the original one.
  •       Soft link has different permission from the original one.
  •       Soft Link can link between directories.
  •       Soft Link is the link of the main file not content of the main file.

What is Hard Link and its feature in Linux

Hard Link is the copy of the main file. If we remove the main file we will get content after removing the original one. 
Feature :

  •         Hard Link can not cross the file system
  •          Hard Link has same inode no from the original one.
  •          Hard Link has same permission from the original one.
  •          Hard Link can not  link between directories.
  •          Hard Link carry the main content of the original file.

Demonstration of Soft Link in linux with practical example

First we can a directory in which we create the main file and create soft link of this file. Suppose we create a directory named “link” and in the directory we create the main file named “mainfile” and create a soft link named “softlinkfile”. To execute the above tasks follow the below commands.
[root@localhost ~]# mkdir link
[root@localhost ~]# cd link
[root@localhost link]# echo "welcome to TechRider" > mainfile
[root@localhost link]# cat mainfile
welcome to TechRider


Explain Soft Link and Hard Link in linux with practical example

                             Explain Soft Link and Hard Link in linux with practical example


[root@localhost link]# ln -s mainfile softlinkfile
Now our Soft Link is created and we observer the feature to collect the output.

[root@localhost link]# ll -i
total 4
3947 -rw-r--r--. 1 root root 21 Dec  8 14:05 mainfile
5283 lrwxrwxrwx. 1 root root  8 Dec  8 14:06 softlinkfile -> mainfile

From the above output we saw that the inode no is different( one is 3947 and other is 5283) from the main file and permission is also different from the main file.
Before removing the main file when we can the softlinkfile we will see the content of the file.

[root@localhost link]# cat softlinkfile
welcome to TechRider

After removing the main file we will get the below output.

[root@localhost link]# rm -rf mainfile
[root@localhost link]# cat softlinkfile
cat: softlinkfile: No such file or directory

Demonstration of Hard Link in linux with practical example

First we create original file named  “mainfile” and create hard link of this file named “hardlinkfile” to execute the below commands.

[root@localhost ~]# echo "welcome to TechRider" > mainfile
[root@localhost ~]# ln mainfile hardlinkfile
[root@localhost ~]# cat mainfile
welcome to TechRider
[root@localhost ~]# cat hardlinkfile
welcome to TechRider

Now our Hard Link is created and we observer the feature to collect the output.
[root@localhost ~]# ll -i
total 32
 3947 -rw-r--r--. 2 root root    21 Dec  8 14:19 hardlinkfile
 3947 -rw-r--r--. 2 root root    21 Dec  8 14:19 mainfile

From the above output we saw that inode and file permission is same from the main file.
After removing the main file we will view the content of the main file. To see this we execute the below commands.

[root@localhost ~]# rm -rf mainfile
 [root@localhost ~]# cat hardlinkfile
welcome to TechRider

If this article is helpful please share this article.





SHARE

Admin

  • Image
  • Image
  • Image
  • Image
  • Image
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment