Hard Links and Soft Links With Detailed Example In Ubuntu Linux

In Linux, a link is a way to reference the same file by more than one name. There are two kinds: hard links and soft links (also called symbolic links or symlinks). They look similar at first, but they behave very differently under the hood.

In this guide, we’ll explain both types with detailed examples and sample output on Ubuntu Linux.

Understanding inodes

To understand links, you first need to understand the inode. Every file on a Linux filesystem has an inode—a data structure that stores the file’s metadata (permissions, owner, size, timestamps) and, crucially, the location of its data blocks on disk. The file name is just a label that points to an inode.

You can see the inode number of a file with ls -i:

ls -i original.txt
1234567 original.txt

For more, see how to get a file’s inode number in Linux.

hard link is a second name that points to the same inode as the original file. Both names are equal—there is no “original” and “copy.” They share the same data on disk, so a change through one name is visible through the other.

Key traits of hard links:

  • They share the same inode number.
  • They cannot span different filesystems.
  • They normally cannot be created for directories.
  • The data survives until all hard links are removed.

First, create a file:

echo "Hello, Linux" > original.txt

Now create a hard link with ln (no options):

ln original.txt hardlink.txt

Check both with ls -li (the -i shows the inode, -l the details):

See also: Mastering the Linux Command Line — Your Complete Free Training Guide

ls -li original.txt hardlink.txt
1234567 -rw-r--r-- 2 user user 13 Sep  6 10:00 hardlink.txt
1234567 -rw-r--r-- 2 user user 13 Sep  6 10:00 original.txt

Notice two things:

  • Both files have the same inode number (1234567).
  • The link count is now 2 (the number after the permissions), meaning two names point to this inode.

Because they share data, editing one changes the other:

echo "Second line" >> hardlink.txt
cat original.txt
Hello, Linux
Second line

soft link is a special file that simply stores the path to another file. It has its own inode and its own tiny bit of data—the target’s path. Think of it like a shortcut. If the target is moved or deleted, the symlink is left pointing at nothing (a “broken” or “dangling” link).

Key traits of soft links:

  • They have a different inode from the target.
  • They can span different filesystems.
  • They can point to directories.
  • They break if the target is removed or moved.

Use ln -s (the -s means symbolic):

ln -s original.txt softlink.txt

Check with ls -li:

ls -li original.txt softlink.txt
1234567 -rw-r--r-- 2 user user 13 Sep  6 10:00 original.txt
2345678 lrwxrwxrwx 1 user user 12 Sep  6 10:01 softlink.txt -> original.txt

This time:

  • The inode numbers are different (1234567 vs 2345678).
  • The file type is l (the first character of lrwxrwxrwx), marking it a link.
  • The -> original.txt shows where the symlink points.

You can confirm the target with readlink:

readlink softlink.txt
original.txt

What happens when you delete the original

This is where the two types differ most. Let’s remove the original file:

rm original.txt

The hard link still works, because the data lives with the inode, and the inode isn’t freed until its link count reaches zero:

cat hardlink.txt
Hello, Linux
Second line

The soft link is now broken, because its target path no longer exists:

cat softlink.txt
cat: softlink.txt: No such file or directory

A broken symlink is easy to spot with ls -l—many terminals highlight it in red, and it still shows the (now invalid) -> target. For safe cleanup of stray files, see how to delete files in Linux.

FeatureHard linkSoft link (symlink)
InodeSame as originalIts own inode
Points toThe file’s data (inode)The file’s path
Across filesystemsNoYes
Link to a directoryNo (normally)Yes
If original is deletedStill worksBreaks (dangling)
Created withln src linkln -s src link
File type in ls -l- (regular)l (link)

When to use each

  • Use a hard link when you want two truly equivalent names for the same data on the same filesystem, and you want the data to persist even if one name is removed.
  • Use a soft link when you need to link across filesystems, link to a directory, or create a shortcut that clearly points to a known path—for example, /usr/bin/python -> python3.

Summary

Hard links and soft links both let you reference a file by more than one name, but they work differently:

  • hard link is another name for the same inode and data.
  • soft link is a separate file that stores the path to a target.

Use ls -li to compare inode numbers and link counts, and readlink to see where a symlink points. For a related take, see hard links and symbolic links in Ubuntu Linux, and to review the ls output fields, see understanding the ls command in Linux.

Avatar photo
David Cao

David is a Cloud & DevOps Enthusiast. He has years of experience as a Linux engineer. He had working experience in AMD, EMC. He likes Linux, Python, bash, and more. He is a technical blogger and a Software Engineer. He enjoys sharing his learning and contributing to open-source.

Articles: 275

Leave a Reply

Your email address will not be published. Required fields are marked *