Hard Link vs Soft Link in Linux | Explained

In Linux OS, every information is stored in the respective inode for a file which gives all the aspects of data of the file. There are two kinds of creating links in Linux, Hard links and soft links, that are used for creating the links to the files. It is just like pointer creation in the programming languages such as c++, JavaScript, etc.

In this post, the comparison between hard links and soft links will be briefly explained. The write-up’s content is as follows:

Let’s get started.

What is Hard Link in Linux?

A hard link generates a copy of the original file and acts as a physical mirror for the selected file, which accesses all the information. The syntax for creating the hard link in Linux is given below:

Syntax:

$ ln [source File] [File to Create the Link]

The represents the file whose link is to be created, and the [File to Create the Link] refers to the copied file (or the hard link of the source file).

How is Hard Link Created in Linux?

An example is created to create a hard link of the file named “source.txt”. Before that, let’s have a look at the content of the file:

$ cat source.txt

We have a source.txt file that can be seen.

Now, the “ln” command provided below will create a hard link of the file named “source.txt” and will save it as “hardlink.txt”:

$ ln source.txt hardlink.txt

The successful execution of the command ensures that the link has been created.

Verifying the Hardlink

It can be verified by retrieving the content of the file “hardlink.txt” as follows:

$ cat hardlink.txt

The output shows that the same content is copied into the file.

Moreover, the “ls” command can also be used to verify the presence of the newly created file “hardlink.txt”:

$ ls

Permissions:

Moreover, the inode(index node) permissions of both files are the same, which can be seen using the following command:

$ ls -l source.txt hardlink.txt

In the above image, it can be seen that permissions for the files are the same.

Why is Hard Link Effective in Linux?

The main advantage of creating the hard link is that if we delete the original file, it will not influence the output of the hard link file. For instance, we removed the source file named “srouce.txt” and used the “ls” command to check that the hard link is present or not:

$ rm source.txt
$ ls
$ cat hardlink.txt

The output of the “ls” command ensures that the hard link is present in the current directory.

Let’s move and understand the concept of soft links.

What is Soft Link in Linux?

A soft link is also named a symbolic link. Soft links work exactly like the pointer in modern programming languages. It generates a copy of the original file but is not accurate because when the original file is deleted, the user will not be able to access the source file’s content. The syntax for the soft link is the same as the hard link.

How is a Soft Link Created in Linux?

Let’s understand the soft link with an example. Before proceeding, let’s check the content for the source file whose soft link will be created.

$ cat source.txt

The above command shows that we have a source.txt file.

Now, the “ln” command provided below will create a soft link of the file named “source.txt” and will save it as “softlink.txt”:

$ ln -s source.txt softlink.txt

After executing the above command, the soft link file is pointing to the source file. Moreover, there will be an “arrow” icon on the file, which can be seen in the below screenshot:

Verifying the SoftLink

Use the “ls” command to verify the presence of the newly created file “softlink.txt”:

Now, check the output for both files. To do so, use the cat command:

$ cat source.txt
$ cat softlink.txt

Permissions:

Moreover, the inode permissions of the both file are not the same, which can be seen using the following command:

$ ls -l source.txt softlink.txt

The above image shows that the soft link file is pointing to the source file and permissions for both files are different.

Why is Soft Link Not Effective as Hard Link?

The main disadvantage of creating the soft link is that if we delete the original file, the user will not be able to access the output of the soft link file. For instance, the following “rm” command will remove the main file, and the deletion is verified using the “ls” command:

$ rm source.txt
$ ls
$ cat softlink.txt

In the above image, we can see that the soft link file is not able to access the source file.

Note: Want to get more information on hard and soft links? Click here to read out our article.

That’s it from this guide.

Conclusion

In Linux, hard and soft links are used to create the links between the files. The difference between these two links is hard links can access the original file’s output after deleting the original file, while soft or symbolic links don’t have this feature and are deleted with the source file. In this post, hard links and soft links are briefly demonstrated using examples.