ln from Zero: Hard Links and Symlinks — How One File Gets Two Names
One set of contents can have another doorway without copying anything. This guide starts from zero on ln: hard links are a second name sharing an inode, symlinks store the target's name; why relative symlinks break, why hard links cannot cross filesystems or link directories. Every step was run for real on GNU coreutils 9.4.

A file's name and its contents are actually two different things. It feels like one file is one name plus one set of contents, but the name is really just a label stuck on the contents: the same contents can carry several labels, or carry a slip of paper that says "go look over there". The ln command does exactly that: without copying any contents, it gives a file an extra name, or leaves a shortcut pointing at another file. This guide starts from zero on ln: what hard links and soft links are, how to make them, and the two real traps beginners fall into. Every step was run for real on GNU coreutils 9.4, and the transcript is included at the end; every line of output quoted here matches that record word for word.
First, why links exist at all. Copying a file is the obvious approach, but copying has a price: a big file copied takes up a second share of space, and once you edit one copy the other is out of date. A link copies no contents; it only adds another doorway in. Whichever doorway you walk through, you see the same contents, and an edit in one place shows up everywhere. Config files shared between two projects, a large dataset shared between several experiments, a tool that needs to change version without its path changing — these are jobs for links, not copies. Learning ln means learning how to give one set of contents several doorways.
1. Hard links: a second name for the same data
A hard link is what ln makes by default: ln original.txt hard.txt. With no options at all, ln makes a hard link. After that, original.txt and hard.txt are two names for the same file. How do you prove it? Look at the inode number. The filesystem gives each set of data an inode number, and a name is just a label pointing at an inode. In the demo, stat -c '%i %n' original.txt hard.txt printed the same inode number on both lines; the more direct proof is the check in the script itself: same inode: YES. Two names, one inode, one set of data.
Since it is one set of data, editing through either name changes the same thing. The demo appended a line, second line, to hard.txt, and then read original.txt: both lines were there, first line and second line. Edit original.txt and hard.txt changes too. The two names are fully equal — there is no "original" and no "copy". original.txt came first, but that does not make it more genuine. Delete it, and hard.txt still works.
Deleting a file means something different in the world of hard links. rm original.txt removes only the name original.txt, not the data itself. After deleting it in the demo, cat hard.txt still read first line and second line, and ls still showed hard.txt sitting there. When does the data actually go away? When the last name pointing at it is deleted. stat -c '%h %n' shows that count: after the hard link was made, both original.txt and hard.txt showed 2, meaning "two names point at this data"; after original.txt was deleted, hard.txt showed 1, only one name left; delete that last one and the data is finally freed. That count is the filesystem's ledger for whether data is still alive.
A hard link also takes no extra space. Nothing is copied, only a name is added, and all the disk gains is one more directory entry. Sharing a dataset of several gigabytes between two experiment directories would take half a day to copy and twice the space; a hard link finishes instantly and takes one share of space. That is the most practical benefit of hard links. But do not overuse them: with many hard links around, deleting a file gets confusing — you think you deleted a file, but the data is still alive under another name.
How do you tell quickly whether two names are hard links to the same data? ls -i is the handiest way: it prints the inode number in front of each filename, and if the numbers match, they are hard links. stat -c '%i %n' does the same thing with cleaner output, which makes it better inside scripts. There is an even more direct check: the number from stat -c '%h' is the link count — 2 means two names, 1 means only one is left. Whenever you suspect "are these two files the same data?", check the inode numbers first, then the link count. The two numbers together give you the answer.
2. Symlinks: storing the target's name
A symlink, short for symbolic link, is a different thing: ln -s real.txt link.txt. The -s stands for symbolic. After that, link.txt is not a second name for real.txt. It is a separate little file that holds one line of text: real.txt. Open link.txt and the system reads that line, goes and finds real.txt, and hands you its contents. In the demo, cat link.txt printed real content, exactly as if you had read real.txt directly — except for one extra hop of "follow the name to find it".
How do you recognise link.txt as a symlink? ls -l shows it at a glance: link.txt -> real.txt, with the target after the arrow. stat can tell them apart too: stat -c '%F %n' link.txt real.txt printed symbolic link link.txt and regular file real.txt — one is the symlink type, one is the regular file type. readlink link.txt is more direct still: it prints only the target's name, real.txt. Each of the three has its use when debugging: ls -l shows where it points, stat shows the type, and readlink gives just the target name, which is handy for feeding into a script.
There is one more thing in that ls -l line that is easy to misread: the permissions column for a symlink always shows lrwxrwxrwx, as if everyone could do everything. That string of permissions is decoration. The GNU manual says it plainly: on the GNU system, the mode of a symlink has no significance and cannot be changed. What actually counts is the target file's permissions: reading and writing through the link are allowed or refused according to the target. Do not chmod a symlink when you see lrwxrwxrwx — chmod follows the link and changes the target's permissions, while the link itself does not move. If you need to test "is this a symlink?" in a script, use test -L link.txt: true means it is a symlink, and that is far more reliable than parsing ls -l output.
The biggest difference between symlinks and hard links is what happens when the target goes away. A hard link's target is always there, because the name itself is part of the data. A symlink's target is only a name, and if the file it names is deleted or moved, the link breaks and dangles. The demo made a link pointing at missing.txt, which does not exist at all — and ln -s created it without complaint, because creating a symlink does not check whether the target exists. Then cat broken.txt reported cat: broken.txt: No such file or directory, with exit code 1. The link itself is fine; what it points at does not exist, and reading it gives you that error. Remember this: ln -s does not inspect the goods when it builds the link. The inspection happens when you read through it.
3. The first real trap: a relative symlink is read from where the link lives
This is the first real trap in this guide, and the place where symlinks catch people most often. A relative symlink is interpreted relative to the directory that contains the link — not relative to the directory you were standing in when you typed the command. In the demo, from the working directory, the command was ln -s data.txt sub/link2.txt: data.txt was right there in the working directory, link2.txt was created successfully, and yet cat sub/link2.txt reported cat: sub/link2.txt: No such file or directory, exit code 1. ls -l sub/link2.txt showed sub/link2.txt -> data.txt — the link itself was fine. The problem was the name stored inside it: link2.txt lives inside sub/, so the relative name data.txt is looked up from inside sub/, and there is no data.txt in sub/.
The fix is to write the target as a path that works starting from the link's own directory: ln -sf ../data.txt sub/link2.txt. .. means "one level up": from sub/ go up one level, then find data.txt, and that works. After the fix, cat sub/link2.txt printed data. The -f means "if the destination already exists, remove it first and then create the new link": without -f, ln refuses because sub/link2.txt already exists; with -f, it moves the old link aside and builds the new one. You will use -f every day when you change where a link points, so remember it.
The GNU coreutils manual offers advice that prevents this trap: before creating a relative symlink, first cd into the directory where the link will live, and then create it. That way the target name you type in that directory is the same name the link will resolve to later, and the two cannot drift apart. If all that moving around is a nuisance, use ln -sr instead: -r makes ln work out the relative path from the link's location to the target by itself. In the demo, ln -sr real.txt sub/link3.txt produced readlink sub/link3.txt showing ../real.txt — ln computed the correct relative path on its own, so you do not have to count .. entries in your head. The -r flag is small, but it hands the easiest step to get wrong over to the machine.
4. The second real trap: hard links cannot cross filesystems or link directories
Hard links have two hard limits, both of which the demo ran into on purpose. The first is that they cannot cross a filesystem boundary. A hard link is, at heart, "one more name for the same inode", and an inode number only means something inside one filesystem — outside it, the numbers do not line up. The demo tried to link real.txt to a spot in /dev/shm: /dev/shm is tmpfs, a different filesystem from the working directory, and ln answered ln: failed to create hard link '/dev/shm/demo278-ln-hard.txt' => 'real.txt': Invalid cross-device link, exit code 1. "Cross-device" means "across devices", which is to say across filesystems. USB sticks, external drives, network mounts, and mounted volumes inside containers are all different filesystems, and hard links to any of them fail with this same error. When you see it, do not doubt yourself — switch to a symlink instead. A symlink stores a name, and names do not care about filesystems.
The second limit is that directories cannot be hard-linked. The demo made a directory with mkdir -p dir1 and then ran ln dir1 dirlink, which reported ln: dir1: hard link not allowed for directory, exit code 1. Filesystems refuse hard links to directories because directories linked to each other freely could loop the whole tree into a circle, and many tools that walk directories would spin forever inside it. When a directory needs a second doorway, use a symlink: ln -s dir1 dirlink2 worked on the first try, ls -l dirlink2 showed dirlink2 -> dir1, and readlink dirlink2 printed dir1. Symlinks link directories with no trouble at all, and the "current version" links in projects — the ones that point at a directory — are all symlinks.
5. Absolute or relative: writing symlinks so they survive a move
The target name stored in a symlink can be an absolute path or a relative one, and choosing wrong means it breaks when things move. An absolute path is steady: no matter where the link is moved, it always points at the same file. Its weakness is that it is fixed: move the whole project directory to another machine, and the chain of directories in that absolute path may simply not exist there, so the link breaks. A relative path is the opposite: as long as the link and the target move together and keep their relative positions, the link survives; but move the link somewhere else on its own and the relative path points at the wrong place.
Choose by asking whether the link and its target are a "move together" pair. Links inside a project — say a config inside the project pointing at shared/config inside the same project — should be relative: the whole project moves together, gets passed to someone else together, and goes into version control together, so the relative positions never change. Links pointing at system locations — say a tool in /usr/local/bin linking to a program in /opt — should be absolute: system locations do not move with the project, so the absolute path is the steadier choice. The coreutils manual says the same thing in its own words: an absolute link always points at the same file, even if the directory containing the link is moved, while a relative link is resolved in relation to the directory that contains the link and works well when the device is reached under different mount names.
One more thing most people never notice: a symlink itself takes almost no space, because all it stores is the string of the target's name. In ls -l, the size column for a symlink shows the length of that string: the link.txt line showed 8, because real.txt is eight characters, and the sub/link2.txt line also showed 8, because data.txt is eight characters too. Do not be surprised when a symlink's "size" is only a few bytes — it was never storing data in the first place.
6. Deleting links, changing targets, linking directories: everyday moves
Deleting a symlink uses the same command as deleting a regular file: rm link.txt. What gets deleted is the link itself; the target is untouched. In the demo, after link.txt was deleted, cat real.txt still printed real content — the target was perfectly fine. This is the move beginners fear most: will rm link.txt delete real.txt too? No. rm deletes a name. The name link.txt is gone, the name real.txt is still there, and the data is of course still there. The other way round is different: rm real.txt deletes the target, and link.txt instantly becomes a dangling link — reading it again gives No such file or directory. Before deleting, be clear about whether you are removing a doorway or the data itself. Doorways can be deleted freely; deleted data is really gone.
Change where a link points with ln -sf: -s for symlink, -f to remove the old one first and build the new one. In the demo, sub/link2.txt was repointed with ln -sf ../data.txt. Before changing a target, look at where it points now with readlink: readlink sub/link2.txt printed ../data.txt, and only after confirming that should you change it. Change it blind and you will not know what went wrong. Run readlink once more afterwards, compare the two outputs, and you know whether the change landed.
Linking directories is one of the everyday uses of symlinks. Projects often have a link called current pointing at the current version directory: ln -s v2.3 current, and every program goes through the current doorway. When version v2.4 arrives, ln -sfn v2.4 current switches the doorway over in one line. The -n means "treat current as an ordinary file; do not dive into the directory it points at to build the link": without -n, ln might create the new link inside the directory current points to instead of replacing current itself. A directory link with -sfn is the most common three-letter combination for switching versions.
Here is one more use you will meet every day as a vibe coder: config files. Many people keep their configuration in a repository called dotfiles — ~/dotfiles/.bashrc, ~/dotfiles/.gitconfig, and the like — managed in one place. On a new machine, git clone brings the repository down, but the configuration still has to be copied into the home directory one file at a time. Symlinks exist for exactly this: ln -s ~/dotfiles/.bashrc ~/.bashrc turns the .bashrc in the home directory into a link pointing into the repository. Edit the configuration in the one place, the repository; commit it, push it, pull it down on another machine, rebuild the link, and everything is back. Writing the link with a relative path is steadier here: move the whole repository and the link moves with it, the relative positions stay the same, and nothing breaks. This one use combines "configuration in the repository" and "configuration in the home directory" into a single thing, and it is the most useful symlink skill to learn first.
7. Hard link or symlink: how to choose
Choose by what you need. If you want "delete the original name and the data stays, save space, edit in one place and see it everywhere", take a hard link: backing up a big file without doubling its space, or giving one set of data a second doorway, is where hard links shine. If you want "cross filesystems, link directories, see where it points", take a symlink: switching tool versions, sharing configuration files, and shortcut doorways into project directories are where symlinks feel natural. There is also a quick test: can ls -l show you where it points at a glance? If yes, it is a symlink; if you cannot tell and need stat and an inode number to confirm, it is a hard link. Symlinks are visible, hard links are hidden, and that visibility itself is a reason to choose: when other people need to see where something points, use a symlink.
One more situation deserves its own mention: a version control repository can hold symlinks but not hard links. Git records a symlink's target name, so after cloning, the link is still there. A hard link in git is just two separate files — after cloning there are two sets of contents, editing one leaves the other unchanged, and everything that made the hard link meaningful is gone. So links inside projects should always be symlinks; keep hard links for local work on one machine. Once you separate "goes into the repository" from "stays on this machine", you will not discover after cloning that your links have quietly "stopped working".
A final warning about backups: backup tools treat the two kinds of links differently. Some store each name of a hard link as its own copy, so restoring produces two separate sets of contents. Some store a symlink as the target file itself, rather than as a link. Before you trust a backup, check how your backup tool handles links, and after restoring, spot-check a few links with ls -l and stat to confirm they are still links and still point at the right place. A backup only counts if what comes back is the same as what went in — if the links come back changed, the restore is not a success.
8. Checklist: follow this the next time you use ln
First, ask: do I want "another name for the same data" or "a shortcut pointing at another file"? The first is ln, the second is ln -s. Second, verify right after creating it: for a hard link, use stat -c '%i' to check the two inode numbers match; for a symlink, use ls -l to check the arrow points the right way and readlink to see the target name. Third, work out relative symlinks starting from the directory the link lives in, and if the counting gets tricky, let the machine do it with ln -sr. Fourth, a hard link across filesystems reports Invalid cross-device link — switch to a symlink; a hard link to a directory reports hard link not allowed for directory — switch to a symlink. Fifth, rm deletes a link, which is a doorway, not data; before deleting a target, think about which links point at it. Sixth, only symlinks go into version control; hard links stay local.
Test record: every step above was run for real on GNU coreutils 9.4, and the full output is in demo278_ln.sh and demo278_ln_transcript.txt (94 lines). ln is not hard. The hard part is telling the two kinds of pointing apart: a hard link points at an inode, a symlink points at a name. The inode road shares the data, survives deleting a name, but cannot cross filesystems and cannot link directories. The name road is flexible, visible, crosses disks, and links directories — but once the target moves, it dangles. Walk both roads once, and the next time you see an arrow like link.txt -> real.txt, you will know exactly what happened behind it.
Comments (0)
Sources
Related articles
cron is the scheduling daemon that has run on Linux for decades: write a crontab file saying 'run this command at this time', and it handles the rest. This guide runs everything for real on Debian cron 3.0pl1-162 — installing a job, waiting one minute for the trigger, verifying the log, removing the job — plus special strings, five classic mistakes, and a real 3 a.m. log-archiving job.
ping does exactly one thing: send an ICMP echo request, wait for the echo reply, and report the round-trip time and packet loss. This guide runs every command for real on iputils ping 20240117 — packet count, interval, quiet mode, packet size, timestamps, IPv4 vs IPv6, the three failure modes, exit codes 0/1/2 — with output pasted verbatim.
cut does one thing: split each line into fields on a delimiter and output the fields you ask for. This guide runs every command for real on GNU coreutils 9.4 — field lists and ranges, CSV and output delimiters, character slicing, --complement, the -s rule for lines without a delimiter, log-column statistics, and the quoted-comma limit — with the output pasted verbatim.