Skip to content
Tutorials Center
My learning

Topics

Lesson 12 of 95 ยท Working with Files

Hard Links, Symbolic Links and Inodes

Understand inodes, hard links and symbolic links on Linux: how ln and ln -s work, link counts, dangling links, readlink, relative symlinks and links to directories.

What is a file name? It seems obvious: the name is the file. On Linux it is not. A file name is only a label in a directory that points to the real object, and one object can have several labels, or none that you can see. Once you understand this, links become simple, and several things that confuse beginners (why moving a file is instant, why deleting a big file does not always free space, why an ln -s link can break) fall into place.

This lesson explains inodes, then the two kinds of links: hard links, which are extra names for the same file, and symbolic links (symlinks), which are small files that contain a path. It follows Finding Files and Copying, Moving and Deleting, and leads into File Permissions.

Inodes: the file behind the name

A Linux filesystem stores every file as two parts:

  • the data blocks, with the content;
  • an inode (index node), a small record holding everything about the file except its name: size, owner, group, permissions, timestamps, a link count, and where the data blocks are.

A directory is just a table that maps names to inode numbers. When you run cat notes.txt, the system looks up notes.txt in the directory, finds an inode number, and reads that inode.

TEXT
 directory "docs"                    inode table                 data blocks
 +--------------+---------+        +---------------------+      +-------------+
 | name         | inode   |        | inode 5821          |      |             |
 +--------------+---------+        |  owner, mode, size  |----->| file bytes  |
 | notes.txt    |  5821   |------->|  times              |      |             |
 | copy.txt     |  5821   |------->|  link count = 2     |      +-------------+
 | other.txt    |  5977   |--+     +---------------------+
 +--------------+---------+  |     | inode 5977 ...      |
                             +---->|                     |
                                   +---------------------+

Two names pointing to inode 5821 are two hard links to the same file. The inode's link count says how many names it has. The file's data is released only when the count reaches 0 and no process still has the file open. That is what rm really does: it removes a name and decrements the count.

ls -i shows inode numbers. Their values differ from machine to machine, so we compare them instead of printing them.

Bash Inodes and link counts

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

ln EXISTING NEW creates a hard link. There is no "original" and no "copy": both names are equally real. You can delete either; the data lives on while at least one name remains. A hard link costs only a directory entry, no extra disk space for data, which is why backup tools such as rsync --link-dest and Git's object store use them to keep many snapshots cheaply.

Limits of hard links

  1. They can only point to files on the same filesystem, because an inode number is only meaningful within one filesystem.
  2. They cannot point to directories (except the system's own . and ..). Allowing it would let users create loops in the tree.
  3. They cannot tell you which name is "the original". To find all names of a file use find . -samefile NAME or find . -inum NUMBER.
Bash Hard links: find them and the directory restriction

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

In the output, sub/b.txt still says changed while the new a.txt says brand new: after the mv, b.txt is linked to the old inode (now named old-a.txt), and the new a.txt is a different file. The last part matters in practice. A command that writes into the file (echo >, >>, cat >) keeps the inode, so all hard links see the change. Many editors and tools (and sed -i) instead write a new file and rename it over the old one, which creates a new inode and silently breaks the link. When your program relies on hard links being in sync, check how the editor saves.

A symbolic link (symlink, soft link) is a different thing. It is a tiny file with its own inode whose content is a path. When a program opens a symlink, the kernel reads the stored path and continues there. ln -s TARGET LINKNAME creates one; note the order: what it points to first, the link's name second, the reverse of what you may expect from cp.

Bash Symbolic links: ln -s, readlink and ls -l

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

In ls -l, a symlink shows l as the file type, the permission string lrwxrwxrwx (always; the real permissions are those of the target), and name -> target. Our awk trimmed that line to the parts that are stable between runs. The size of a symlink is simply the number of characters in the path it stores (app-1.0.txt is 11).

Notice that some tests (-f, -d, -e) follow the link, while -L (or -h) asks whether the name itself is a link.

Because a symlink only stores a path, nothing stops the target from vanishing. Then the link is dangling (broken). It still exists, but opening it fails with No such file or directory, which confuses people because ls shows the link right there.

Bash A dangling symlink (and how to find it)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

find . -xtype l is a GNU way to list broken symlinks (-xtype tests the type of what the link points to; when the target does not exist it cannot be resolved, so the type stays l). On BSD/macOS use find -L . -type l. The last lines show a feature of symlinks: they work by name, so if a new file appears under that name, the link heals.

Relative and absolute targets

The stored path can be absolute (/opt/app/v2) or relative (../shared/config). A relative target is resolved from the directory that contains the link, not from where you ran ln. This is the single most common symlink mistake.

Bash Relative targets are relative to the link

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

The wrong link stored shared/config.ini, which would be looked up as project/shared/config.ini, and failed. The correct form ../shared/config.ini works from anywhere, and because it is relative the whole tree can be moved or copied without breaking. Absolute targets break as soon as the directory moves. GNU ln -r (--relative) writes the right relative path for you; macOS ln does not have it.

Symlinks may point to directories (hard links may not). A symlink to a directory behaves like the directory in most commands, which is great for "current release" shortcuts, as in deployments:

TEXT
releases/2026-01-10/     releases/2026-02-02/     current -> releases/2026-02-02

Updating current atomically gives zero-downtime deploys. ln -sfn NEWTARGET current replaces the link (-f removes the existing link, -n stops ln from treating an existing directory-link as a directory to put the new link into). The deployment project in this tutorial, A Deployment Script Skeleton, uses exactly this pattern.

Bash Swapping a directory symlink

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

The second command, ln -sf releases/v2 current, did not replace the link: because current already pointed to a directory, ln created releases/v1/v2 inside it, and current still showed v1's content. With -n (--no-dereference) the link itself is replaced. After cd current, pwd -P prints the physical path and plain pwd the logical one the shell remembers.

Hard link Symbolic link
What it is Another name for the same inode A small file containing a path
Command ln target name ln -s target name
Cross filesystems No Yes
Link to a directory No Yes
If the original is deleted Data lives on Link dangles
If the original is moved Still works (same inode) Breaks (path no longer valid)
Shows as Normal file, link count > 1 l type, name -> target
Own permissions Shared with the file Irrelevant (target's apply)

Practical advice: use symlinks by default (they are visible, flexible and cross-filesystem); use hard links for deduplication and snapshots where "no dangling, no original" is the point.

How other tools treat symlinks: cp -a copies the link itself, plain cp copies the target, rm removes the link only, ls -L shows the target's details, find -L follows links (and may loop), and tar stores links as links by default.

Symlinks are a classic attack tool: a privileged program that writes to /tmp/report can be tricked into overwriting /etc/passwd if an attacker pre-creates /tmp/report as a link to it. Use mktemp for temporary files, and never follow user-controlled links in privileged code.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. A file has two hard links, a and b. You run rm a. What happens to the data?

    A directory entry is just a name for an inode. Removing a name decrements the link count; the data is freed only when the count reaches 0 and no process has the file open.
  2. 2. In ln -s ../shared/config.ini project/config.ini, what is the ../shared/config.ini part relative to?

    A relative symlink target is resolved from the link's own directory each time it is followed, not from where ln was run.
  3. 3. Which of these is NOT possible with a hard link?

    Hard links to directories are not allowed (to avoid loops). Linking regular files and sharing an inode are exactly what hard links do.
  4. 4. What does ls -l show as the size of a symlink to app-1.0.txt?

    A symlink is a small file whose content is the target path, so its size equals the length of that path string.
  5. 5. current is a symlink to a directory. Which command safely repoints it to releases/v2?

    -f replaces an existing link and -n stops ln from entering the directory the old link points to. Without -n you create a new link inside the old target.

Summary

  • A name is a directory entry pointing to an inode, which holds the metadata and data location. The link count says how many names exist.
  • ln a b makes a hard link: same inode, same filesystem only, no directories, data survives until the last name goes.
  • ln -s target link makes a symlink: a small file storing a path; it can cross filesystems and point to directories, but can dangle.
  • Relative symlink targets are relative to the link's directory; GNU ln -r computes them for you.
  • readlink, readlink -f, stat, find -samefile and find -xtype l inspect links; [[ -L ]] tests for a symlink, -e follows it.
  • Use ln -sfn to swap a symlink to a directory atomically.

Next: File Permissions and Ownership