Hard Links and Symbolic Links, Explained Step by Step
A file link gives you another way to reach a file. On Linux and macOS, a hard link gives the same file a second name. A symbolic link, also called a symlink or soft link, is a small file that points to a path: the location of a file or folder.
At first, reading either link gives the same result. Delete the first filename, though, and one link keeps working while the other breaks. A short terminal experiment shows why.
Start With One Small File
Open a terminal on Linux or macOS. Run the command blocks below in order, in the same terminal window. Blocks marked Output show the result; they aren't commands to paste.
First, make a practice folder and move into it:
mkdir link-demo && cd link-demo
mkdir creates the folder. cd moves you into it. The && means the second command runs only if the first succeeds. If you get a “File exists” error, choose a fresh folder name in both places before continuing.
- Stop breaking your focus to look up a command: the answer is already under your wrist.
- Find what you need fast, with commands grouped by job from files to networking.
- Enough room for a laptop or a keyboard and mouse, on a soft surface that's easy on your wrists.
Now create a short note:
echo 'Meet at three.' > note.txt
echo prints the words. The > sends them into note.txt instead of onto the screen. It creates the file if needed, or replaces its contents if it already exists. That's why we're working in a fresh folder. The Bash manual explains this output redirection.
Give the Note Two More Names
Make a hard link and a symbolic link:
ln note.txt hard.txt
ln -s note.txt soft.txt
The order is existing file first, new link name second. Plain ln makes a hard link. Adding -s makes a symbolic link, as described in the GNU guide to the link command.
Successful ln commands normally say nothing. Read all three names with cat, which prints file contents:
cat note.txt hard.txt soft.txt
Output, in the order you listed the names:
Meet at three.
Meet at three.
Meet at three.
You have three ways to read the note, but you haven't made three copies of it.
| Name | What it refers to |
|---|---|
note.txt | The note itself. |
hard.txt | The same note, under a second name. |
soft.txt | The path note.txt, which leads to the note. |
Change the meeting time through the hard link:
echo 'Meet at four.' > hard.txt
cat note.txt soft.txt
Output:
Meet at four.
Meet at four.
The change appears through both other names. There's no syncing or copying going on: this command changed the one file they all reach.
Hard Link or Soft Link? Watch the Difference
Delete a Name and See What Survives
Remove note.txt, then read the hard link:
rm note.txt
cat hard.txt
Output:
Meet at four.
The note is still there. rm removed one name for it, while hard.txt remains another valid name. Although we created note.txt first, it has no special status. Neither hard link is the “main” one.
Try the symbolic link:
cat soft.txt
Output on a typical English-language system:
cat: soft.txt: No such file or directory
This error is part of the demonstration. soft.txt still exists, but it points to note.txt, a path that no longer leads anywhere. That's a broken, or dangling, symbolic link.
The soft link doesn't search for the surviving file under hard.txt. It follows the path it was given. You can read that stored path with readlink:
readlink soft.txt
Output:
note.txt
That small piece of text is all the link needs to remember. Here, note.txt is a relative path: it's resolved from the folder containing soft.txt. Moving just the link to another folder can therefore break it. The Linux documentation on symbolic links explains how relative paths work.
Bring Back the Name
Create a new file called note.txt, this time with a different message. Then read the soft link and the hard link:
echo 'Lunch at noon.' > note.txt
cat soft.txt hard.txt
Output:
Lunch at noon.
Meet at four.
The soft link works again without being changed. Its path now leads to the new file. The hard link still reaches the earlier note.
This is the difference the matching output hid at the start: a hard link refers to a particular file; a symbolic link refers to a path. Put a different file at that path, and the symbolic link leads there instead.
Behind the scenes, the system tracks a file through an inode, a record containing information such as its owner and permissions. Hard links to the same file share an inode. Inode numbers identify files within one filesystem, the system that organizes files on a storage volume. They aren't unique across the whole computer. The Linux inode reference describes these records.
A Second Name Isn't a Spare Copy
Hard links are useful when you want the same file available under more than one name, perhaps in different folders. On Linux and macOS, those names must stay on the same filesystem, and ordinary hard links are for files, not directories.
Symbolic links can point to directories and across filesystems. For example, a link in your home folder could lead to a project on an external drive. The link would stop working while that drive was unavailable. These differences are covered in the Linux guide to hard and symbolic links.
What Really Happens When You Type ls -l?
Neither kind of link is a backup. The meeting-time experiment showed why: changing the file's contents through one name changed what the others read. A hard link doesn't preserve yesterday's version or protect the data from a failed drive.
There's one wrinkle when you use a text editor. Some editors save by creating a replacement file and swapping it into place. That's closer to our delete-and-recreate step than to writing into the existing file. A hard link can then keep the older file while the edited filename reaches the new one. The Vim documentation on saving linked files describes this choice.
- Finally understand what the commands you type are doing behind the scenes.
- Fix problems yourself instead of pasting in fixes you found online and hoping.
- Go from following tutorials to knowing why they work, one clear chapter at a time.
To clear away the experiment, run these commands from inside the link-demo folder. If you chose another folder name at the start, use it in the last line:
rm note.txt hard.txt soft.txt
cd ..
rmdir link-demo
Here, removing soft.txt removes the link itself. Removing the last hard link lets the system reclaim a file's space once no program has it open, as the Linux file-removal documentation explains. Finally, rmdir removes the empty practice folder.
Symbolic Links: A Filename That Leads Somewhere Else

Unix: A Family History, a Standard and a Set of Permissions

The Shell Between Your Command and the Computer

QUOTE:
"Amateurs hack systems, professionals hack people."

