How to verify the creation of a hard link in Linux

LinuxLinuxBeginner
Practice Now

Introduction

In the Linux file system, hard links provide an efficient way to reference the same file content under multiple names. This tutorial will guide you through the concept of hard links, how to create and manage them, and explore their practical implications for your Linux system.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux/BasicFileOperationsGroup -.-> linux/cat("`File Concatenating`") linux/BasicSystemCommandsGroup -.-> linux/echo("`Text Display`") linux/BasicSystemCommandsGroup -.-> linux/test("`Condition Testing`") linux/BasicFileOperationsGroup -.-> linux/ls("`Content Listing`") linux/BasicFileOperationsGroup -.-> linux/ln("`Link Creating`") subgraph Lab Skills linux/cat -.-> lab-409957{{"`How to verify the creation of a hard link in Linux`"}} linux/echo -.-> lab-409957{{"`How to verify the creation of a hard link in Linux`"}} linux/test -.-> lab-409957{{"`How to verify the creation of a hard link in Linux`"}} linux/ls -.-> lab-409957{{"`How to verify the creation of a hard link in Linux`"}} linux/ln -.-> lab-409957{{"`How to verify the creation of a hard link in Linux`"}} end

In the Linux file system, a file is represented by an inode, which contains the file's metadata, such as permissions, ownership, and timestamps. A hard link is an additional directory entry that points to the same inode as the original file. This means that multiple file names can refer to the same underlying data on the file system.

When you create a hard link, you are not creating a copy of the file; instead, you are creating an additional reference to the same file. This has several important implications:

  1. Space Efficiency: Since hard links share the same inode and data blocks, they do not consume additional storage space on the file system.

  2. Deletion: Deleting a hard link does not delete the file itself; the file will only be deleted when the last hard link to it is removed.

  3. File Modification: Modifying the file through any of its hard links will affect the original file, as they all point to the same inode and data.

Here's an example of creating and working with hard links in Ubuntu 22.04:

## Create a file
touch original_file.txt

## Create a hard link
ln original_file.txt hard_link.txt

## Verify the hard links
ls -l
## Output:
## -rw-rw-r-- 2 user user 0 Apr 12 12:34 original_file.txt
## -rw-rw-r-- 2 user user 0 Apr 12 12:34 hard_link.txt

## Modify the file through one of the hard links
echo "Hello, world!" >> hard_link.txt

## Verify the modification
cat original_file.txt
## Output:
## Hello, world!

In this example, we first create a file called original_file.txt. We then use the ln command to create a hard link called hard_link.txt that points to the same inode as original_file.txt. When we list the files, we can see that both files have a link count of 2, indicating that they are hard links to the same file.

Finally, we modify the file through the hard_link.txt hard link, and we can see that the changes are reflected in the original_file.txt file as well, as they both point to the same underlying data.

Creating hard links is a straightforward process using the ln command in the Linux terminal. The basic syntax is:

ln <source_file> <link_name>

Here, <source_file> is the existing file you want to create a hard link for, and <link_name> is the name of the new hard link.

For example, to create a hard link named backup.txt for the file original.txt, you would run:

ln original.txt backup.txt

Once the hard link is created, you can manage the files just like any other files in the file system. For instance, you can view the file details, modify the contents, or delete the files.

## View file details
ls -l
## Output:
## -rw-rw-r-- 2 user user 0 Apr 12 12:34 original.txt
## -rw-rw-r-- 2 user user 0 Apr 12 12:34 backup.txt

## Modify the file through a hard link
echo "Hello, world!" >> backup.txt

## Delete a hard link
rm backup.txt
## The file is still accessible through the original link
cat original.txt
## Output:
## Hello, world!

In the example above, we first create a hard link backup.txt for the original.txt file. We then modify the file through the backup.txt hard link, and the changes are reflected in the original.txt file as well. Finally, we delete the backup.txt hard link, but the file content is still accessible through the original.txt link.

It's important to note that hard links cannot cross file system boundaries, meaning you cannot create a hard link for a file located on a different file system or partition.

To verify and explore hard links, you can use various Linux commands and tools. One of the most commonly used commands is ls -l, which displays detailed file information, including the link count.

## Create some hard links
ln original.txt hard_link1.txt
ln original.txt hard_link2.txt

## List the files and their hard link counts
ls -l
## Output:
## -rw-rw-r-- 3 user user 0 Apr 12 12:34 original.txt
## -rw-rw-r-- 3 user user 0 Apr 12 12:34 hard_link1.txt
## -rw-rw-r-- 3 user user 0 Apr 12 12:34 hard_link2.txt

In the example above, we can see that the original.txt file and its two hard links (hard_link1.txt and hard_link2.txt) all have a link count of 3, indicating that they are referencing the same underlying inode.

Another useful command for exploring hard links is stat, which provides detailed information about a file's metadata, including the inode number.

## Check the inode number of a file
stat original.txt
## Output:
##   File: original.txt
##   Size: 0         Blocks: 0          IO Block: 4096   regular empty file
##   Device: 801h/2049d    Inode: 12345678    Links: 3
##   Access: (0664/-rw-rw-r--)  Uid: (1000/username)   Gid: (1000/username)
##   Access: 2023-04-12 12:34:56.123456789 +0000
##   Modify: 2023-04-12 12:34:56.123456789 +0000
##   Change: 2023-04-12 12:34:56.123456789 +0000
##    Birth: -

In this example, we can see that the original.txt file has an inode number of 12345678 and a link count of 3, confirming that it has two hard links.

By understanding the inode numbers and link counts, you can effectively verify and explore the relationships between hard links in your Linux file system.

Summary

Hard links in Linux allow you to create multiple directory entries that point to the same underlying file data, without consuming additional storage space. By understanding how hard links work, you can leverage them to optimize file management, ensure efficient use of disk space, and maintain consistency when modifying files. This tutorial has covered the key aspects of hard links, from creation and verification to the practical considerations of using them in your Linux environment.

Other Linux Tutorials you may like