Skip to Sidebar Skip to Content

Linux - Create and Manage Hard Links

Linux - Create and Manage Hard Links

Highlights

  • Core Foundation: Get a clear inode explanation to understand exactly how Linux tracks your data blocks and file metadata beneath the surface.
  • Smart Storage: Learn to use Linux hard links to share large files across different directories without duplicating the actual data or consuming extra disk space.
  • The Key Command: Master the ln command Linux provides natively to instantly generate hard links for any target file.
  • Important Distinctions: Understand the difference in a soft link vs hard link scenario, as hard links point directly to an inode while soft links point only to a file path.
  • System Boundaries: Recognize the limitations of these file system links, notably that hard links cannot cross into different file systems or link to directories.

The Linux file system is a hierarchical structure that organizes files and directories on a computer. It starts with the root directory, which is the top-level directory in the file system. From there, directories and subdirectories can be created to organize files into groups. Each file and directory on the file system is represented by an inode, which contains information about the file's ownership, permissions, and location on the disk. One of the features that Linux uses to manage data efficiently is Linux hard links.

This blog will explore what we mean by hard links definition, their incredible benefits, and how to create hard links Linux administrators rely on every day.

Linux File System

To understand hard links and soft links, we first have to learn some very basic things about filesystems.

Let’s imagine a Linux computer is shared by two users: alex and jane. Alex logs in with their own username and password, Jane logs in with her own username and password. This lets them use the same computer, but have different desktops, different program settings, and so on. Now Alex takes a picture of the family dog and saves it into /home/alex/Pictures/family_dog.jpg.

Let’s simulate a file like this.

echo "Picture of Milo the dog" > Pictures/family_dog.jpg

With this, we created a file at Pictures/family_dog.jpg and stored the text “Picture of Milo the dog” inside. There’s a command on Linux that lets us see some interesting things about files and directories.

stat Pictures/family_dog.jpg

We’ll notice an Inode number. What is this? Here is a simple inode explanation:

Filesystems like xfs, ext4, and others, keep track of data with the help of inodes. Our picture might have blocks of data scattered all over the disk, but the inode remembers where all the pieces are stored. It also keeps track of metadata: things like permissions, when this data was last modified, last accessed, and so on. But it would be pretty inconvenient to tell your computer, “Hey, show me inode 52946177”. So we work with files instead - the one called family_dog.jpg in this case. The file points to the inode, and the inode points to all the blocks of data that we require.

And we finally get to what interests us here.

A Hard link is a powerful feature of the Linux file system that can be used to create multiple references to a single file. When comparing a soft link vs hard link, remember that symbolic (soft) links are simply pointers to another file's path, whereas Linux hard links are actual, direct references to the file's underlying inode itself.

Back to our example: we notice this in the output of our stat command. There’s already one link to our Inode? Yes, there is. When we create a file, something like this happens:

We tell Linux, “Hey save this data under this filename: family_dog.jpg”

Linux says: “Ok, will group all this file’s data under inode 51221169. Data blocks and inode created. Will hard link file “family_dog.jpg” to Inode 51221169.

Now when we want to read the file:

“Hey Linux, give me data for family_dog.jpg file”

“Ok, let me see what inode this links to. Here’s all data you requested for inode 51221169”

family_dog.jpg -> Inode 51221169

Easy to understand. But why would we need more than one hard link for this data?

Well, Jane has her own folder of pictures, at /home/jane/Pictures. How could Alex share this picture with Jane? The easy answer, just copy /home/alex/Pictures/family_dog.jpg to /home/jane/Pictures/family_dog.jpg. No problem, right? But now imagine we have to do this for 5000 pictures. We would have to store 20GB of data twice. Why use 40GB of data when we could use just 20GB? So how can we do that?

Instead of copying it, we could simply create hard links Linux systems natively support to link the data directly to /home/jane/Pictures/family_dog.jpg.

To accomplish this, we use the standard ln command Linux distributions provide. The syntax of the command is:

ln path_to_target_file path_to_link_file

The target_file is the file you want to link with. The link_file is simply the name of this new hard link we create. Technically, the hard link created at the destination is a file like any other. The only special thing about it is that instead of pointing to a new inode, it points to the same inode as the target_file.

In our imaginary scenario, we would use a command like:

ln /home/alex/Pictures/family_dog.jpg /home/jane/Pictures/family_dog.jpg

Or, if we’re already inside the /home/alex directory (that’s our current/working directory) we can use a relative path to our target file:

ln Pictures/family_dog.jpg /home/jane/Pictures/family_dog.jpg

Our picture is only stored once, but the same data can be accessed at different locations using different filenames.

First, hard links allow for data stored once to be accessed at different locations using different filenames without consuming double the storage.

Another beautiful thing about hard links is this: Alex and Jane share the same 5000 pictures through hard links. But maybe Alex decides to delete his hard link of /home/alex/Pictures/family_dog.jpg. What will happen with Jane's picture? Nothing, she'll still have access to that data. Why? Because the inode still has 1 hard link to it (it had 2, now it has 1).

But if Jane also decides to delete her hard link /home/jane/Pictures/family_dog.jpg, the inode will have 0 links to it. When there are 0 links, the data itself will be erased from the disk.

The beauty of this approach is that people that share hard links can freely delete what they want, without having a negative impact on other users that still need that data. But once everyone deletes their hard links to that data, the data itself will be erased. So data is “intelligently removed” only when EVERYONE involved decides they don't need it anymore.

Below are the limitations of using hard links:

  • You can only hard link to files, not directories.
  • You can only hard link to files on the same filesystem. If you had an external drive mounted at /mnt/Backups, you would not be able to hard link a file from your SSD, at /home/alex/file to some other file on /mnt/Backups since that's a different filesystem.

To learn more about Linux, check out our hands-on Linux course.

Learning Linux Basics Course & Labs | KodeKloud

Best Practices When Hard Linking

When working with hard links in Linux, there are a few best practices that you should keep in mind to ensure that you are using them effectively and safely.

First, it's important to understand that when you create a hard link, you are essentially creating a second reference to the same file on your file system. This means that any changes you make to the file using one filename will be reflected in the file accessed via any other linked filename. As a result, it's important to be careful when working with hard links to avoid accidentally making unwanted changes to your files.

Second, it's important to ensure that all users involved in accessing a hard-linked file have the necessary permissions to do so. This may require adding users to the same group and changing permissions on the file accordingly.

Finally, when working with hard links, it's important to be aware of the potential for inadvertently creating circular links, which can cause issues with your file system. To avoid this, it's generally a good idea to keep hard links within a single directory and avoid linking files across different directories.


More on Linux:


FAQs

Q1: What exactly are Linux hard links?

Linux hard links are essentially multiple directory entries (filenames) that point directly to the exact same underlying data block (inode) on a hard drive.

Q2: How do I create hard links Linux systems can recognize?

You use the ln command Linux provides by default. Simply run ln <target_file> <link_name>. Do not use the -s flag, as that creates a symbolic (soft) link instead.

Q3: What is the difference between a soft link vs hard link?

A soft link (or symbolic link) points to a file's path (like a Windows shortcut) and breaks if the original file is moved or deleted. A hard link points directly to the data's inode, meaning the data remains perfectly intact even if the original filename is deleted.

Q4: Can you give a brief inode explanation?

An inode is a data structure on a Linux filesystem that stores all the metadata about a file (such as permissions, timestamps, and physical block locations) except its name and its actual data content.

Q5: Are there restrictions on where I can place file system links?

Yes. You cannot create hard links to directories, and you cannot create a hard link that crosses file system boundaries (for example, linking a file from your computer's main SSD to an external USB drive).

Mumshad Mannambeth Mumshad Mannambeth
Mumshad is passionate about sharing his knowledge on DevOps and Cloud & Automation technologies. He believes the best way to learn is to learn by doing and in a fun way.

Subscribe to Newsletter

Join me on this exciting journey as we explore the boundless world of web design together.