Hard and symbolic links
A file's name is separate from its data. Hard links share an inode, symlinks store a path, and sed -i can quietly break a hard link.
Listing and inspecting filesCreating, copying, moving and removing
Hard and symbolic links
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- A directory entry is a name pointing at an inode, which holds the file's data and metadata. One inode can have several names.
- A hard link (ln a b) is another name for the same inode. Both names are equal, and the data lives until the last name is removed.
- A symbolic link (ln -s target link) is a tiny file that stores a path. If the target is removed, the link dangles and reading it fails.
- A relative symlink target is resolved from the link's own directory, not from where you ran ln. Use ../ or an absolute path to point out of a subdirectory.
- Hard links cannot point to directories. Editors and sed -i often replace a file with a new inode, which quietly breaks a hard link.
A name is not a file
When you run cp a b you get two files that start equal and then drift apart. Sometimes you want something else: one file reachable under two names, so that a change through one shows up in the other. That needs a distinction the everyday word “file” hides.
Unix separates a file’s name from the file itself. The file itself is an inode: a record holding the data’s location, size, owner, permissions and times. A directory is just a table of names, each pointing at an inode number. The name isn’t in the file. Think of a library: the inode is the book on a shelf, and each name is a card in a catalogue pointing at the shelf position. Two cards can point at one book, and the book is only thrown away when the last card is.
Hard links: two cards, one book
ln original.txt hard.txt adds a second name for the same inode. ls -i shows inode numbers, and the third column of ls -l counts the names:
$ printf 'v1\n' > original.txt
$ ln original.txt hard.txt
$ ls -li original.txt hard.txt
61421949 -rw-r--r--@ 2 me wheel 3 Nov 23 02:45 hard.txt
61421949 -rw-r--r--@ 2 me wheel 3 Nov 23 02:45 original.txt
(me stands in for my real username.) Both lines carry the same inode number, 61421949, and the link count in the third column is 2. Neither name is the “real” one. Append through one and read through the other:
$ echo 'v2' >> hard.txt; cat original.txt
v1
v2
Delete one name and the data survives:
$ rm original.txt; head -1 hard.txt
v1
$ ls -l hard.txt
-rw-r--r--@ 1 me wheel 6 Nov 23 02:45 hard.txt
The link count fell to 1. When it reaches 0, and no program has the file open, the space is freed. This is why rm is really “remove this name”.
Symbolic links: a note with a path
ln -s target link creates a different kind of file. It has its own inode, and its contents are only the text of a path:
$ ln -s original.txt soft.txt
$ ls -li soft.txt
61421952 lrwxr-xr-x@ 1 me wheel 12 Nov 23 02:45 soft.txt -> original.txt
The leading l is the type, and the size, 12, is the length of the string original.txt. Opening soft.txt makes the system look up that path and use whatever is there. Because it stores a path, not an inode, deleting the target breaks it:
$ rm original.txt
$ cat soft.txt
cat: soft.txt: No such file or directory
$ test -e soft.txt && echo exists || echo dangling
dangling
$ test -L soft.txt && echo "is a symlink"
is a symlink
The link is dangling. Note cat blamed soft.txt, which does exist, so the message is confusing. test -e follows the link and says no; test -L asks about the link itself. A 10-character target gave a symlink size of exactly 10 in my test, which confirms it’s just the path.
The relative-path trap
A symlink’s target is read from the link’s own directory, not from where you typed the command. This breaks more links than anything else:
$ ln -s original.txt sub/bad
$ cat sub/bad
cat: sub/bad: No such file or directory
$ ln -s ../original.txt sub/good
$ cat sub/good | head -1
EDITED
(It prints EDITED because my test script had already run the sed step from the next section.)
sub/bad stores original.txt, and no such file exists inside sub. ../original.txt goes up first. An absolute path (ln -s "$PWD/original.txt" sub/abs) works too and also survives moving the link, but it breaks if the target moves. readlink prints a link’s stored text. Related surprise: ln -s original.txt sub/ made sub/original.txt containing the text original.txt, which resolves to itself. I did that by accident, and ls -l showed original.txt -> original.txt.
When a hard link silently stops being one
I used sed -i '', BSD’s in-place edit (GNU’s is -i with no argument), to change v1 in the re-linked file:
$ ln hard.txt original.txt # the same two names again, one inode
$ sed -i '' 's/v1/EDITED/' original.txt
$ head -1 original.txt; head -1 hard.txt
EDITED
v1
$ ls -li original.txt hard.txt
61421949 -rw-r--r--@ 1 me wheel 6 Nov 23 02:45 hard.txt
61421954 -rw-r--r--@ 1 me wheel 10 Nov 23 02:45 original.txt
After the edit the two names had different inodes. sed -i writes a new file and renames it over the old name, so the name now points at a new inode. Many editors do the same. The old content stayed behind the other name. The lesson: a hard link only shares data until a tool replaces the file instead of modifying it. Symlinks don’t have this problem.
The other limits
Hard links cannot point at directories, and the shell says so: ln sub subhard printed ln: sub: Is a directory. A symlink to a directory works fine (ln -s sub subsoft; ls subsoft listed the contents). Hard links also can’t cross filesystems, since inode numbers are only unique within one (the standard rule, untested). Re-pointing an existing link needs -f (ln -s original.txt soft.txt failed with “File exists”, ln -sf hard.txt soft.txt worked).
Copying a symlink with plain cp copies the target’s contents (the copy was a regular file); cp -P copies the link itself.
About the video
CodeLucky’s “Linux Links Explained: Hard vs Symbolic (Soft) Links Tutorial for Beginners!” was published 8 April 2025 and runs 7 minutes 31 seconds. Its description and chapters promise this lesson’s scope: what inodes are and how hard links reference them (0:16), creating hard links (1:17) and symbolic links (2:20), the ln options -s, -f, -i, -v and -r (3:21), “Managing and Identifying Links” (4:31) and use cases (5:56). English per its title and description. The channel publishes auto-dubbed tracks, so that is judged from text, not by ear. I couldn’t read a transcript, so Dangling links, the relative-path trap and editors breaking hard links are from my own runs. It replaced a 2017 CBT Nuggets video whose page I could not read. Ahmed Alkabary’s 2017 video is the second view.
What I did not run
Linux. Hard links across filesystems, ln -n, and find -L with dangling links. The rm behaviour of an open file with zero links.
Next
Names and links are one half of finding files. The other half is describing groups of names without typing them all: globbing and wildcards.
Check yourself
You create a hard link hard.txt to original.txt and delete original.txt. What happens to the data?
Nothing. Both names pointed at the same inode, so the data stays reachable through hard.txt until its last name is removed. The link count drops from 2 to 1.
You delete the target of a symbolic link. What does cat on the link do?
It fails with 'No such file or directory'. The link still exists as a stored path, but there is nothing at the end of it.
You ran ln -s original.txt sub/bad from the parent directory and the link is broken. Why?
The target text original.txt is resolved relative to sub/, where no such file exists. Write ln -s ../original.txt sub/bad or give an absolute path.
How can you tell that two names are hard links to the same file?
ls -i shows the same inode number for both, and ls -l shows a link count above 1.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.