Monday, November 4, 2024
HomeReviewsHard Links and Soft Links in Linux: Everything You Need to Know   

Hard Links and Soft Links in Linux: Everything You Need to Know   

The term linking in Linux is defined as creating a file path or file location URL in your filesystem. There are two types of linking in Linux, soft links and hard links. The notion of soft links is very much common and easy to understand. If you’re familiar with the shortcut file of Windows, you would think of the soft link as the shortcut of a specific file. The soft links do not carry the entire file but only contain a file name or file details. It only carries the index node (inode) of the file for making the internal linking with that file on the filesystem.

Hard Links and Soft Links in Linux


The hard link is the link or path of the original file with actual size. On the other hand, the soft link only creates a shortcut URL or path for a file or folder in the system. In this post, we will see a detailed explanation of soft and hard links and how linking works on Linux.

1. Soft and Hard Link with Inode


In Linux and Unix-like operating systems, the soft link indicates the link to the original file. If your file has a unique index number, you can create multiple soft links for that original file, and every soft link will refer to the main file.

In most cases, the soft links are also called symbolic links. Creating the soft link of a file is easy with the terminal command shell. The below-mentioned command will guide you through creating a soft link.

$ ln -s targetFile SourceFiles

create soft link

These soft links are often used to refer files inside a command or indicate a node inside a command. In server management, the soft and hard links are often used instead of the original paths.

symbolic url

The hard link is a bit complex to understand and use. As I said earlier, it is another name for a file that contains the metadata and the original index number. We can put the hard link in any directory. With the below terminal command, we can create a hard link to a file.

$ ln hard_link_target  hard_link_output

In speaking of linking on Linux, with advanced ln command, we can look into how many index nodes numbers are linked with the file and the user details. In hard linking, the number of index numbers also indicates the total number of hard links of that file.

The interesting fact about the hard links is, if you delete the original file, the hard link will still remain on the filesystem. The only way to delete a file on Linux is to ensure that there is only one inode number of that file before you delete the file.

Deleting both soft and hard links won’t delete the original file from the Linux filesystem. In some cases, the inode number can help to recover the accidentally deleted files on Linux.

hard link in linux

2. Facts About Hard Links and Soft Links in Linux


At this step, we will see some facts and working methods of soft links and hard links on Linux. Understanding the linking notion on Linux might help you to create links better.

  • Soft links or symbolic URLs are the shortcuts of a file on the Linux system.
  • If the original file is moved or removed, the soft link path will not function properly.
  • You can use the ls -l command to display the symbolic links.
  • You can use the soft links to run commands, link to a directory, or use inside a script.
  • After creating a soft link of a file, when you change the name of that file, you will also need to update the soft link name. Otherwise, the soft link won’t function.
  • The soft links are mostly used to interlink files and directories on the system.

As we have seen a few facts about the symbolic links, there are some facts about the hard links as well that you might need to know.

  • Like soft links, you can use the ls -l command to display the list of hard links on the system.
  • Unlike the soft links, the hard links have the same original file. If you delete either one of them, the rest of it will function.
  • Deleting hard links won’t affect the main file on your Linux filesystem.
  • The size of the hard link is conventionally the same as the main file.
  • You can create hard links for files, but you can not create hard links for directories on Linux and Unix-like OS.
  • The index node value of the original file and index node of the hard link is always the same and also refer to the same file on the filesystem.

3. Pros and Cons of Soft and Hard Links


In the points mentioned above, we have seen the features, work methodology, and properties of how the hard and soft links work on the Linux system. Here we are about to discover the pros and cons of these two file systems.

The main advantages of the soft link or the symbolic link are, you can just create a soft link with no effort, and it does not take a huge amount of size or block on the disk. And in addition, the soft link also allows us to create symbolic links for any files and directories.

The advantage of the hard link is as the file and the hard link both use the same index node, and they take the same amount of space on the system, so in that case, we are actually keeping both files on the file system. So if you accidentally or intentionally remove or delete the main file, the hard link will still be executable.

Now, if we look at the cons of the soft link and the hard link, we can see both have some limitations. For soft links, if you remove, rename, or delete the main file, the soft link won’t function anymore.

4. What Would Happen If We Delete the Main File


You might have been thinking about what will happen if we willingly or mistakenly delete the main file; will the hard and soft links still be functional? Here, we have created a soft link for the target folder. Now, if we delete the main target directory, the system will suggest removing the soft link on the trash on the next execution of that soft link.

We have used the below command to create a soft link of the ‘target’ file. Now, we will delete the main file, and we will see how it impacts the symbolic link.

ln -s target softlink

soft link active

And for hard links, the only disadvantage is you can not create a hard link for any directory on your Linux system. Moreover, it also doesn’t allow you to create hard links for different filesystems.

the symbolic is deleted

Final Words


In the entire post, we have seen a detailed explanation of how soft and hard links work on Linux. In case you intend to create a hard link to a directory, please be note that on modern computers, it’s almost impossible to create a hard link of directories. But you can create soft links to directories.

I hope this post has been helpful for you; if yes, please share this post with your friends and the Linux community. You can also let us know which linking you find most useful, or you’ve been using most on your system.

Mehedi Hasan
Mehedi Hasan
Mehedi Hasan is a passionate enthusiast for technology. He admires all things tech and loves to help others understand the fundamentals of Linux, servers, networking, and computer security in an understandable way without overwhelming beginners. His articles are carefully crafted with this goal in mind - making complex topics more accessible.

You May Like It!

Trending Now