How to Create a Hard Link in Ubuntu Linux Using the ln Command

In Linux, a “link” is a way to create a shortcut or a pointer to an existing file. While most users are familiar with “symbolic links” (soft links), which act much like Windows desktop shortcuts, Linux offers a much deeper, more fundamental type of connection called a “hard link.” A hard link does not just point to a file name; it points directly to the underlying physical data (the inode) on the hard drive. This means multiple hard links are essentially the exact same file, sharing the same data block. If you delete one hard link, the data remains intact as long as at least one other hard link exists. You can create these powerful connections in Ubuntu using the ln command.

The Concept of Hard Links

Before creating a hard link, it is crucial to understand two strict limitations:

  1. You cannot hard link directories. Hard links can only be created for individual files. Attempting to hard link a folder will result in an error.
  2. You cannot hard link across different file systems. Both the original file and the new hard link must exist on the exact same hard drive partition. You cannot hard link a file on your main SSD to your external USB drive.

Creating a Hard Link

The syntax for the ln command is simple. You specify the target (the existing file) and the link name (the new file you are creating).

  1. Open your terminal.
  2. The basic command structure is: ln [target_file] [link_name]
  3. For example, let’s say you have a file named report.txt in your current directory, and you want to create a hard link to it named backup_report.txt.
  4. Type the following command:
    ln report.txt backup_report.txt
  5. Press Enter.

Unlike commands that copy files, ln executes silently if successful. You will simply be returned to the command prompt.

Verifying the Hard Link

To prove that these two files are indeed hard linked to the exact same physical data, you can use the ls command with the -i (inode) flag.

Type: ls -li

Look at the output for both report.txt and backup_report.txt. The very first number on each line is the inode number. You will see that both files share the exact same inode number. This confirms they are pointing to the exact same block of data on your hard drive.

Furthermore, if you edit report.txt and add new text, then open backup_report.txt, you will see those changes instantly reflected, because they are fundamentally the same file.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.