File Management

How to Create Hard Links Linux?

Linking is an interesting feature in Linux. In UNIX-like operating systems, everything is a file. A file is fundamentally a link to an inode, a special type of data structure that stores everything about a certain file except its original name and actual contents.This is the feature that links leverages. To create a link is to create another file that points to the same underlying inode as another file. In many situations, it’s an incredibly useful method.

Check out how to create hard links in Linux.

Hard links in Linux

Before diving deeper, let’s talk a bit about hard and soft links. There are some fundamental differences between them. In the case of a hard link, it can only exist in the same filesystem, while the symbolic link will persist cross-filesystems. Moreover, it can only be performed on regular files. You also can’t create directory hard links, so it doesn’t create a directory loop.

If one file of the hard link is deleted, it removes the link from the underlying inode.

The ls command can print the inode of a target file/directory.

 $ ls -li <file_or_dir>

Creating hard links

Generating a hard link is quite simple. To create links, ln is the dedicated tool available in almost all Linux distros.

Use the following command structure to create a hard link. Note that the destination should be within the same filesystem. The “-v” flag is for verbose mode.

 $ ln -v <source> <link>


Verify the result.

 $ ls -li <target>


As for directory hard linking, it’s not allowed. Hypothetically, it’s still possible to create, but most Linux distros disable that feature, even if you run the action with root privilege. For directory linking, use soft links.

Soft links

Soft linking is commonly referred to as symbolic links. Soft linking can be cross-filesystem. By definition, it’s not a standard file, but rather, a file that points to an existing file. Here, the soft link file will have a different inode value, but it points to the original file.

Just like creating hard links, we’ll be using the ln tool. To declare to create a soft link, add the “-s” flag.

 $ ln -vs <source> <destination>


Verify the result.

 $ ls -li <target>

If there’s already a link existing, then you may update it using the “-f” flag that forces ln to update the link without any confirmation. Alternatively, you could use the “-i” flag for interactive link creation.

 $ ln -sf <source> <destination>


Don’t forget to verify the result.

 $ ls -li <target>

Finding links

Assuming there are multiple links to the same file, keeping track of them can become difficult. In such a situation, use this method to find out all the links.

For this, we need the inode value of the original file. Use the following command to find out the inode number.

 $ ls -li <target_file>


Now, use the inode number to find out all the links to that file. Here, the current active directory should be the directory where the original file is located.

 $ find . -inum <inode_value>

Deleting links

If you want to disable a hard link, the way to do so is by deleting the linked file.

 $ rm <link>

Final thoughts

Linking is a powerful tool you can use in many situations. While it comes with its own limitations, it can offer excellent benefits to lots of scenarios.

Interested in more in-depth about the ln command? Check out how to use the ln command.

Happy computing!

About the author

Sidratul Muntaha

Sidratul Muntaha

Student of CSE. I love Linux and playing with tech and gadgets. I use both Ubuntu and Linux Mint.