$_ CLI In Depth
Learning progress 0%
lesson filesystem inodes navigation

Hard Links vs Symbolic Links

5 min read Module 2: Filesystem Navigation & Inodes Updated Dec 5, 2026
Cheat Sheet

Hard links: two names for one inode

A hard link is created with ln and no option. It adds a directory entry that points at an existing inode. Nothing is copied. Both names have the same inode number, the same permissions, the same size, and the same data, and each name is equally valid. The link count in the inode becomes two. Removing one name leaves the other working, and the data is freed only after the last name goes away and the last open descriptor closes.

There are limits. A hard link cannot cross a filesystem boundary, because an inode number is meaningful only on one filesystem and the directory entry must refer to an inode that lives there. Ordinary users cannot hard link a directory, because it would create cycles in the tree. The kernel keeps the single special case of . and .., which are hard links that the filesystem maintains.

BASH4 lines
cd /workspace
echo "shared data" > original.txt
ln original.txt second-name.txt
ls -li original.txt second-name.txt

Both lines show the same inode number, and the link count printed by ls -l is two. If you edit either file, you edit the single copy of the data. If you rename one, the other is unaffected.

Symbolic links: a path stored as data

A symbolic link is created with ln -s. It has its own inode, with the file type set to a link, and its data is the text of a path. When the kernel resolves a path that passes through the link, it substitutes the stored text and continues the walk. The link has no knowledge of the target inode, so the target can be on another filesystem, can be a directory, or can be deleted and replaced without the link noticing.

The stored text can be absolute or relative. A relative target is interpreted relative to the directory that contains the link, not relative to your current directory when you run a command. This rule causes many broken links, because the link works when you test it from one place and fails when a script runs from another.

BASH4 lines
cd /workspace
ln -s original.txt soft-name.txt
ls -l soft-name.txt
readlink soft-name.txt

The long listing shows the type l and an arrow to the target text. The readlink command prints the stored text without following it.

Dangling links and what they reveal

A symbolic link whose target does not exist is called dangling. The link still exists, and ls -l still shows it, but reading through it fails with "No such file or directory". You can test the difference between a dangling link and a missing name with -L, which is true for a symbolic link even when its target is missing, and -e, which follows the link and is false when the target is absent.

BASH4 lines
cd /workspace
ln -s missing-target.txt broken.txt
test -L broken.txt && echo "link exists"
test -e broken.txt || echo "target is missing"

Dangling links often appear after a directory is moved or a package is removed. The fix is to recreate the target or to remove the link. Do not assume that a link is valid just because ls shows it.

What a copy does and does not share

A copy with cp creates a new inode with its own data blocks. Changing the copy never changes the original, and the two files can later diverge. A hard link is different, because it shares the inode, so there is only one file and two names. The option cp -l creates hard links instead of copies for each file it would have copied, which is the trick behind many fast snapshot schemes. The option cp -s creates symbolic links instead.

Editors make the difference visible. Some editors write a new file and rename it over the old name, which breaks a hard link because the old inode is no longer reachable from that name. Others write into the existing inode, which keeps the link intact. If a file must stay linked to another name, check how the editor saves before you rely on the link.

BASH6 lines
cd /workspace
echo one > base.txt
cp base.txt copied.txt
ln base.txt linked.txt
echo two >> base.txt
cat copied.txt linked.txt

The copy still contains only one, while the hard link shows both lines, because they share one inode. This is the practical difference a person needs to remember when deciding between the two.

Choosing between them

Use a hard link when you want two names for the same content inside one filesystem, and when you want the data to survive the removal of either name. Backup tools such as rsync and many deduplicating systems use hard links for this purpose. Use a symbolic link when the target may move, when it lives on another filesystem, when it names a directory, or when you want the link to be visibly a pointer. Most "shortcuts" in a Linux system are symbolic links, because they are flexible and easy to see.

A common mistake is to create a symbolic link to a directory with ln -s dir existing-dir when existing-dir is already a directory. The link is then created inside it, not in place of it. Use ln -sfn to replace an existing link without descending into the directory it names.

Warning

Editing through a hard link changes every name at once, which is often what you want. Editing a file that is reached through a symbolic link changes the target. Deleting the target of a symbolic link leaves the link dangling but does not delete the link.

Practice in the lab

The lab creates a target file in a new directory and then asks you to make a hard link, a symbolic link, and a dangling link. Each step checks the file that you create, so the evidence is on disk where the checker can see it.