File Management

How to Create Hard Links Linux?

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

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


Verify the result.

$ ls -li


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


Verify the result.

$ ls -li

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


Don't forget to verify the result.

$ ls -li

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


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

Deleting links

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

$ rm

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!

Battle For Wesnoth 1.13.6 Development Released
Battle For Wesnoth 1.13.6 released last month, is the sixth development release in the 1.13.x series and it delivers a number of improvements, most no...
Cum se instalează League Of Legends pe Ubuntu 14.04
Dacă ești fan al League of Legends, atunci aceasta este o oportunitate pentru tine de a testa rula League of Legends. Rețineți că LOL este acceptat pe...
Instalați cel mai recent joc de strategie OpenRA pe Ubuntu Linux
OpenRA este un motor de jocuri de strategie în timp real Libre / Free care recreează primele jocuri Westwood, cum ar fi clasicul Command & Conquer: Re...