In Linux, links to files are created in the same way that references to files are created in most common programming languages. These links are divided into two categories: hard and soft links.

A hard link is effectively an identical replica of the file, therefore the hard link and the actual file will both have the same inode.

A soft link, also known as a symbolic link, functions similarly to a shortcut or pointer to a file. It is not an exact replica of the file, but rather a pointer to the original.

The inode values of a soft link to a file and the real file will differ. Furthermore, you will not be able to access the contents of the soft link if you remove the actual file. In this article, we will learn more about soft links and hard links.

What are Soft Links and Hard Links in Linux? Filesystem hard links soft links soft links and hard links
Understand difference between Hard links and Soft links

What are soft links in Linux?

We have a file shortcut feature in windows that is used to create a shortcut for files. A soft link is similar to a file shortcut. Each soft-linked file has its own Inode value, which refers to the original file. Any changes to the information in one file are mirrored in the other. You can connect soft links across different file systems, but if the source file is removed or transferred, the soft-linked file will not function properly. This link is called a hanging link. Removing a soft link has no effect, but If you remove the original file, it may cause the link will stop working.

A soft link may be used to point to a directory and it just retains the path to the original file, not its contents. The size of the soft link will be equal to the file for which the soft link is formed. For example, if the file theabcd.txt is 5KB, the soft link of this file will be also 5KB.

A soft link:

  • may traverse the file system,
  • allow you to connect across directories,
  • has different file permissions and inode than the original file,
  • permissions will not be modified,
  • and has just the original file’s path, not its contents

What are hard links in Linux?

A hard link in Linux is equivalent to a file saved on a hard drive – and it really refers to or links to a location on a hard drive. A hard link is essentially a mirror image of the original file. The difference between a hard link and a soft link is that removing the source file has no effect on a hard link but makes a soft link unworkable. So the most significant benefit of making a hard link is that you can still access the contents of the file even if you unintentionally erase it.

A hard link:

  • can’t traverse file system borders,
  • can’t connect directories,
  • has the same file permissions and inode number as the original file
  • If we alter the permissions of the source file, permissions will be changed for the hard link too
  • contains the content of the original file, even if the original file is destroyed you can access the content.

Difference between Soft Links and Hard Links

Parameter Soft Link Hard Link
Inode number Different inode number than the original file. Same inode number as the original file.
Directory Soft links can link directories Hard links can not link directories across.
Original File deletes The link will not operate if the original file is deleted since it does not access the data in the original file. It’s nothing more than a shortcut to the original file. The Hard link will continue to operate even if the original file is deleted since it accesses the same data as the original.
Speed Soft links are slower Hard links are faster than soft links.
Memory Consumption More Less

Check the below screenshot of Inode differences between Hard link and Soft link files:

What are Soft Links and Hard Links in Linux? Filesystem hard links soft links soft links and hard links
Inode changes in Soft link and Hard link

How to create Hard links and Soft links?

Let’s understand the differences with a quick practice. Create a simple text file in a directory and name it file1.txt. We will create a soft link and a hard link for this file.

Creating Hard Link:

  1. To create a hard link, we use the “ln” command. So type the following command in your terminal window:
    ln file1.txt hardlink.txt 
    
  2. So here we are using the ‘In’ command to create a hard link, then we are giving the name of the file for which we need the hard link, and then we are naming the hard link which is hardlink.txt.

    Type the following command after executing the previous command:

    ls -l 
    
  3. This will show you that there are two files in the directory – file1.txt and hardlink.txt. Now let’s just check if our hard link file is working or not. Open the hardlink.txt file in a text editor:
    nano hardlink.txt 
    
  4. Change the content of the file and write something different. Then open the original file:
    cat file1.txt 
    
  5. You will see that the changes made in the hard link files are reflected in the original file. Now let’s delete the original file and see if the hard link file will still function or not:
    rm file1.txt 
    
  6. Run the “ls” command and you will see that there is only one file in the directory hardlink.txt. Open the file and you will see that the file still has the same content.

Creating Soft Link:

  1. To demonstrate, agai I have created file.txt in current directory. We will create a soft link for the same. Run the following command to create a soft link:
    ln -s file1.txt softlink.txt 
    
  2. Run the ls -l command and you will see that there are two different files in the directory. Now if you will type
    cat softlink.txt 
    
  3. You will see the content of file1.txt as the soft link is pointing to this file. And if you remove the file1.txt file:
    rm file1.txt 
    
  4. Again run the ls -l command and you will see an error. It is because the original file is removed and the soft link cannot work without that. And if we type:
    cat softlink.txt 
    

    You will see that there is no such file or directory

Wrapping up

A hard link takes up less space and works faster, but the modifications made to it are reflected in the original file. Whereas, Soft links need more space, any changes to the soft link have no effect on the original file. Unlike hard links, soft connections to directories are permitted.