Hard Links and Symbolic Links In Ubuntu Linux

Links let you reference one file by more than one name. Linux gives you two kinds: hard links and symbolic links (symlinks, also called soft links). Both are created with the ln command, but they work very differently. This guide focuses on the ln command syntax, real use cases, linking directories, and how to find and fix broken links on Ubuntu.

The ln command syntax

The ln command creates links. Its basic form is:

ln [OPTIONS] TARGET LINK_NAME
  • ln target link creates a hard link named link pointing to target.
  • ln -s target link creates a symbolic link named link pointing to target.

TARGET is the existing file; LINK_NAME is the new name you’re creating.

A hard link is an additional name for an existing file’s inode—the on-disk structure that holds the file’s data and metadata. Two hard links to the same file are indistinguishable equals: they share the same inode number and the same data. (For background on inodes, see how to get a file’s inode number in Linux.)

echo "Ubuntu Linux" > file1.txt
ln file1.txt file2.txt
ls -li file1.txt file2.txt
2621501 -rw-r--r-- 2 user user 13 Sep  6 11:00 file1.txt
2621501 -rw-r--r-- 2 user user 13 Sep  6 11:00 file2.txt

Both names share inode 2621501, and the link count (the number after permissions) is 2. Delete either name and the data remains until the count reaches zero.

Because hard links share an inode, you can find every name that points to it. Use find with -samefile (or -inum):

find . -samefile file1.txt
./file1.txt
./file2.txt
  • They cannot cross filesystems (an inode number is only meaningful within one filesystem).
  • You normally cannot hard-link a directory—this is disallowed to prevent loops in the filesystem tree.

A symbolic link is a small, separate file that stores the path to another file. It has its own inode and works like a shortcut. If the target moves or is deleted, the symlink is left dangling.

ln -s file1.txt link1.txt
ls -li file1.txt link1.txt
2621501 -rw-r--r-- 2 user user 13 Sep  6 11:00 file1.txt
2621502 lrwxrwxrwx 1 user user  9 Sep  6 11:05 link1.txt -> file1.txt

Here the symlink has a different inode (2621502), a file type of l, and an arrow (->) showing its target. Confirm where it points with readlink:

readlink link1.txt
file1.txt

Unlike hard links, symlinks can reference directories—one of their most common uses:

mkdir -p /opt/app/releases/v2.0
ln -s /opt/app/releases/v2.0 /opt/app/current
ls -l /opt/app/current
lrwxrwxrwx 1 root root 24 Sep  6 11:10 /opt/app/current -> /opt/app/releases/v2.0

This “current” pattern lets you switch versions by repointing a single symlink.

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

A symlink can store either an absolute path or a relative one. Relative links stay valid if you move the whole directory tree together:

ln -s ../shared/config.yml config.yml   # relative
ln -s /etc/app/config.yml config.yml    # absolute

Use readlink -f to resolve a symlink to its final absolute path:

readlink -f config.yml

If a target is removed or moved, the symlink becomes “broken” (dangling). Accessing it fails:

rm file1.txt
cat link1.txt
cat: link1.txt: No such file or directory

Find broken symlinks under a directory with find -xtype l:

find . -xtype l
./link1.txt

To fix one, repoint it at a valid target with ln -sf (force overwrite):

ln -sf /path/to/new_target link1.txt

To remove a broken link, delete it like any file (see how to delete files in Linux):

rm link1.txt
FeatureHard linkSymbolic link
What it referencesThe file’s inode/dataThe file’s path
InodeSame as targetIts own inode
Across filesystemsNoYes
Link to a directoryNoYes
If target is deletedStill worksBreaks (dangling)
Created withln target linkln -s target link
ls -l type-l

When to use each

  • Hard link — when you want two equivalent names for the same data on the same filesystem, and you want the data to survive removal of either name.
  • Symbolic link — when you need to cross filesystems, link a directory, or create a clearly labeled shortcut (like /opt/app/current) that you can repoint later.

Summary

Both hard links and symbolic links are made with ln, but a hard link is another name for the same inode, while a symbolic link is a separate file pointing to a path. Use ls -li to compare inodes and link counts, readlink to see a symlink’s target, find -samefile to list hard links, and find -xtype l to hunt down broken symlinks. To review the ls output fields, see understanding the ls command in Linux, and for a step-by-step comparison, see hard links and soft links with detailed examples.

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 *