December 5, 2026

Symbolic Links vs Hard Links in Linux: What Each One Points To

Two blank brass tags tied to the same suitcase handle

A symbolic link to a file called a.txt is exactly five bytes long. Those five bytes are the letters a, period, t, x, t. That’s all a symlink is: a small file holding a path, which the system follows whenever something opens it. A hard link holds no path at all. It’s a second name for the same file, and the system can’t tell which name came first. The two behave identically right up until someone renames, moves or deletes something, and then they behave completely differently. Every command and output below comes from a test run on a Linux machine with GNU coreutils’ ln.

Video: CBT Nuggets on YouTube

Two kinds of name

A hard link is the file

On Linux file systems, a file’s data and metadata live in a structure called an inode, and a file name is an entry in a directory that points to that inode. The GNU coreutils manual defines a hard link as “another name for an existing file; the link and the original are indistinguishable.” The link(2) man page goes further: the new name “may be used exactly as the old one for any operation,” and “it is impossible to tell which name was the ‘original’.”

A symbolic link is a pointer by name

A symbolic link is its own file with its own inode. The symlink(7) man page describes it as “a special type of file whose contents are a string that is the pathname of another file.” The GNU manual puts it as a link that “refers to a different file, by name.” The kernel reads that string and follows it each time.

Creating both

The test started with one file and two links:

echo hello > a.txt
ln a.txt hard.txt
ln -s a.txt soft.txt

Plain ln makes a hard link. Adding -s makes a symbolic one.

A small cabin in the middle of a forest

What ls and stat show

Same inode, different inode

ls -li prints the inode number in the first column:

1155173 -rw-r--r-- 2 root root 6 Dec  1 20:03 a.txt
1155173 -rw-r--r-- 2 root root 6 Dec  1 20:03 hard.txt
1155174 lrwxrwxrwx 1 root root 5 Dec  1 20:03 soft.txt -> a.txt

a.txt and hard.txt share inode 1155173. The symlink got its own, 1155174, an l at the start of its permissions, and an arrow showing its target.

The link count

The number after the permissions is the link count, the number of names pointing at that inode. Both hard-linked names show 2, because each is one of the two names. stat -c '%n inode=%i links=%h size=%s' confirmed it: links=2 for both, links=1 for the symlink. The symlink’s size, 5, is the length of the text “a.txt,” not the six bytes of “hello” plus a newline in the target.

Writes go through both

Appending a line with echo more >> hard.txt and then running cat a.txt printed both lines. There’s one file with two names, so a change through either name is a change to the file.

Rename, move, delete

Renaming the original

The test then ran mv a.txt renamed.txt. The hard link didn’t notice: cat hard.txt still printed both lines, and renamed.txt still reported links=2. The symlink broke immediately. cat soft.txt failed with “No such file or directory,” while ls -l still showed it pointing at a.txt, a name that no longer existed.

Dangling links

symlink(7) has a word for that state: “A symbolic link that refers to a pathname that does not exist is said to be a dangling link.” The symlink itself is fine. Its contents are still the string “a.txt”; there’s just nothing at that path anymore. Running find . -xtype l listed every dangling link in the test directory, which makes it a quick way to audit a tree for broken ones.

Deleting a name

Next came rm renamed.txt. cat hard.txt still printed both lines, and the link count dropped to 1. The unlink(2) man page explains why: unlink “deletes a name from the filesystem,” and only “If that name was the last link to a file and no processes have the file open” is the file actually deleted and its space freed. There is no separate “delete the file” operation. A file disappears when its last name does.

Open files outlive their names

The same man page covers the case that surprises people: if the last name is removed “but any processes still have the file open, the file will remain in existence until the last file descriptor referring to it is closed.” A test opened a file on descriptor 3, deleted it, and confirmed ls could no longer find it. Reading from descriptor 3 still printed “still here.” That’s why deleting a large log file that a running program is still writing doesn’t free the disk space until the program closes or restarts.

Deleting a symlink

The same page says that if “the name referred to a symbolic link, the link is removed.” Running rm on a symlink removes the pointer and leaves the target alone.

Finding every name a file has

Because a hard link carries no arrow, ls gives no hint where a file’s other names are. Two find options answer the question. In the test, find . -samefile hard.txt listed ./hard.txt and ./cpl.txt, and find . -inum with the inode number from stat -c %i returned the same pair. For symlinks the question runs the other way: readlink -f sub/good.txt followed the chain all the way to /tmp/lab/ln/hard.txt, printing the absolute path of the real file at the end. Both checks are worth running before deleting anything that might have a second name, or might be someone else’s second name.

Wooden card catalog drawers with metal labels

The limits on hard links

No directories

ln d dlink on a directory failed with “hard link not allowed for directory.” The GNU manual notes that “Most systems prohibit making a hard link to a directory,” and link(2) lists EPERM when “oldpath is a directory.” Directories do use hard links internally, though. A new, empty directory showed links=2 in stat: its own name in the parent plus its . entry. The parent’s count rises by one for every subdirectory, because each subdirectory’s .. points back to it.

No crossing file systems

The test machine’s root file system and /dev/shm are separate (df showed /dev/vda and tmpfs). ln hard.txt /dev/shm/x.txt failed with “Invalid cross-device link,” the EXDEV error, which link(2) describes as “oldpath and newpath are not on the same mounted filesystem.” That follows from what a hard link is: an inode number only means something within its own file system.

Symlinks have neither limit

symlink(7) says “symbolic links may refer to directories and may cross filesystem boundaries.” The same test with ln -s to /dev/shm worked, and cat through it printed the file. ln -s d dsoft linked the directory without complaint.

The relative path trap

Paths are resolved from the link

A relative symlink is read from where the link sits, not from where the command was run. The GNU manual says a relative link “is resolved in relation to the directory that contains the link.”

Watching it break

From the test directory, ln -s hard.txt sub/bad.txt looked reasonable, since hard.txt was right there. But the link lives in sub/, so it pointed at sub/hard.txt, which didn’t exist. cat sub/bad.txt failed. Writing the target as ../hard.txt worked.

Letting ln do the math

GNU ln has an option for this. -r, or --relative, will “Make symbolic links relative to the link location.” Running ln -sr hard.txt sub/rel.txt and then readlink sub/rel.txt returned ../hard.txt, the correct path, worked out automatically.

Absolute or relative

An absolute target such as /tmp/lab/ln/hard.txt survives the link being moved but breaks if the target’s directory tree is moved. A relative one survives the whole tree being moved together, which is why relative links are common inside projects and packages.

Arrow signs

Choosing between them

Where symlinks fit

Symlinks are the visible, flexible option. They’re how many Linux systems manage interchangeable versions of a program. On the test machine, ls -l /usr/bin/python3 showed it pointing to /etc/alternatives/python3, a second symlink that selects the installed interpreter. Repointing a symlink is one command: ln -sf t2 s replaced the old target, and readlink s returned t2.

Where hard links fit

Hard links suit cases where two paths need to be the same file and stay valid if either name is deleted. Backup tools use that property to store unchanged files once across many snapshots, each snapshot holding another name for the same data. GNU cp -l makes hard links instead of copies; after cp -l hard.txt cpl.txt, the count on hard.txt went back to 2.

The side effect to watch

A hard link is not a backup of the file. Editing through any name changes it for every name. Some editors complicate this by writing a new file and renaming it over the old one, which gives the edited name a new inode and quietly separates it from its other hard links. When a link should always track whatever is at a path, a symlink is the safer choice. When a link should keep the data alive no matter what happens to the original name, use a hard link.

Sources

  • ln invocation, GNU Coreutils manual: https://www.gnu.org/software/coreutils/manual/html_node/ln-invocation.html
  • link(2), Linux manual page: https://man7.org/linux/man-pages/man2/link.2.html
  • symlink(7), Linux manual page: https://man7.org/linux/man-pages/man7/symlink.7.html
  • unlink(2), Linux manual page: https://man7.org/linux/man-pages/man2/unlink.2.html

920 Posts

Peter Kalafatis writes and edits X9M8. He holds a bachelor's degree from LIU Post and a master's in Media Studies from The New School, with academic work in the philosophy of science, human behavior, AI, and memetics.

View All Posts

Leave a Reply

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