How to Create Hard Links Linux?

In Linux, The “Hard links” refer to a “shortcut” that is the connection between the file name and the data on a disk. In simple words, the “Hard Link” is the link/copy of original source files having the same parameters such as “inode value”, “permissions”, and “file size”. They can be made easily in every Linux distribution.

This guide will illustrate the procedural method to create hard links in Linux.

How to Create Hard Links Linux?

To create the “Hard Links”, follow the step-by-step instructions that are described below:

Syntax

The generalized syntax to create a hard link is as follows:

$ ln [original-filename] [link-name]

The syntax contains the following components:

  • ln: The main keyword that showsthe “ln(link)” command used to create the hard links.
  • original-filename: Identifies the source file whose har link is to be created.
  • link-name: Specifies the created hard link or coped of the source file with a new name.

Step 1: Access the Source File

The first step is to access the source file location where it is placed. This is because the created hard link refers to the same file location.

Suppose the “Documents”  directory contains “File1.txt” having file size “319b” as shown by the “ls -l” command:

$ ls -l

Step 2: Create the Hard Link

Now execute the “ln” command in the current directory “Documents” followed by the source file “File1.txt” and the hard link name “FirstHardLink”:

$ ln File1.txt FirstHardLink

The “FirstHardLink” has successfully been created in the “Documents” directory.

Tip: To view the specified action details during the execution of the “ln” command, use its “-v(verbose)” option in the following way:

$ ln -v File1.txt FirstHardLink

Step 3: Verify the Hard Link

For the verification, run the “ls” command followed by “-l(list)” and “i(index node)” flag to show the

newly created “FirstHardLink” details with its inode value:

$ ls -li

The output displays that both the “File1.txt” and “FirstHardLink” file occupies the same size “319b” and the “inode” value “406519”. In addition, the permissions are also the same for both files.

The user can also search out all the hard links associated with the original file by using its “index inode” value with the “find” command:

$ find . -inum 406519

The output shows the original file and the hard link associated with the index value “4096519”.

Bonus Tip: How to Delete the Hard Links?

Once the hard links are created, they can also be quickly deleted. Deletion of the hard link is not dangerous compared to the soft link because if the user deletes the hard link, then the original source file will not delete.

In this case, we deleted the “FirstHardLink” file using the “rm(remove)” command with its “-v” flag:

$ rm -v FirstHardLink

The output confirms that “FirstHardLink” has been removed from the “Documents” directory. Now there is only the source file “File1.txt” in it.

Conclusion

The “Hard Links” in Linux is used to create the connection or the copy of the original file content. These “Hard links” are beneficial if the original source file is corrupted or deleted because they keep the actual content of the source file. To create the “Hard Link”, execute the “ln(link)” command followed by the source file and the new hard link name. This guide has briefly described the complete procedure of creating hard links in Linux.