Symbolic and Hard Links Explained
Linux has two ways to make a file appear in multiple places without copying the data: symbolic links (symlinks) and hard links. They look identical when you use them, but they work differently — and choosing the wrong one will bite you.
The mental model: inodes
Every file on disk has an inode — a metadata record holding permissions, size, timestamps, and the location of the actual data. The filename is just a pointer to an inode.
ls -i myfile.txt
# 1234567 myfile.txt ← the number is the inode
This is why renaming a file is instant — only the directory entry changes, not the inode.
Hard links
A hard link creates a NEW filename that points to the SAME inode as an existing file. Both filenames are equally “real” — the file isn’t deleted until ALL hard links to it are removed.
echo "hello" > original.txt
ln original.txt twin.txt # create hard link
ls -li original.txt twin.txt
# 1234567 -rw-r--r-- 2 alice ... original.txt
# 1234567 -rw-r--r-- 2 alice ... twin.txt
# ^ link count is now 2
# Edit through one — both reflect the change
echo "modified" > original.txt
cat twin.txt # modified
# Delete one — the other still works
rm original.txt
cat twin.txt # still "modified" — file lives on
Limitations of hard links
- Cannot link across different filesystems (different inodes namespaces).
- Cannot link to directories (would create infinite loops).
- If both filenames are in the same place, this is mostly invisible.
Symbolic links (symlinks)
A symlink is a small file that contains a path to another file. It’s a pointer at the filesystem-name level, not the data level.
echo "hello" > target.txt
ln -s target.txt shortcut # the -s makes it a symlink
ls -li target.txt shortcut
# 1234567 -rw-r--r-- 1 alice ... target.txt
# 1234568 lrwxrwxrwx 1 alice ... shortcut -> target.txt
# ^ different inode; the 'l' marks it as a symlink
# Read through symlink — works
cat shortcut # hello
# Delete the target — symlink becomes broken (dangling)
rm target.txt
cat shortcut # cat: shortcut: No such file or directory
Why symlinks are more useful
- Can cross filesystems and disks.
- Can link to directories.
- You can SEE what they point to (
ls -lshows the target). - Editing/replacing the target updates the symlink’s contents automatically.
Real-world uses
Symlinks: latest version of a config or app
# Versioned releases, with 'current' always pointing to live one
/opt/myapp/releases/v1.0
/opt/myapp/releases/v1.1
/opt/myapp/current -> releases/v1.1
# Switch versions atomically
ln -snf /opt/myapp/releases/v1.2 /opt/myapp/current
Symlinks: dotfiles management
# Keep your dotfiles in a git repo, symlink them home
ln -s ~/dotfiles/.vimrc ~/.vimrc
ln -s ~/dotfiles/.bashrc ~/.bashrc
ln -s ~/dotfiles/.config/nvim ~/.config/nvim
Hard links: backups with deduplication
Tools like rsync --link-dest or rsnapshot create incremental backups where unchanged files are hard-linked to the previous snapshot — same data on disk, but appears in every snapshot folder.
Useful commands
ln -s target linkname # create symlink
ln -sf target linkname # force overwrite if linkname exists
ln -snf target linkname # don't follow if linkname is a symlink to dir
ln target linkname # create hard link
readlink linkname # show what symlink points to
readlink -f linkname # resolve all symlinks to absolute path
realpath linkname # same idea
ls -l # symlinks shown with -> target
ls -li # also show inodes
find / -lname "/old/path" # find symlinks pointing to old/path
find / -inum 1234567 # find all hard links to inode 1234567
When to use which
| Need | Use |
|---|---|
| Pointer to a file in a different folder | Symlink |
| Versioned releases with “current” pointer | Symlink |
| Linking to a directory | Symlink (only option) |
| Cross-filesystem linking | Symlink |
| Backup deduplication | Hard link |
| Multiple “real” names for the same file | Hard link |
Common mistakes
- Broken symlinks when you delete or move the target. Use
ls -lLto spot them, orfind / -xtype l. - Relative vs absolute paths in symlinks:
ln -s ../foo baronly works if you don’t movebar. Use absolute paths for stability. - Hard linking before editing: writing to one hard link affects all of them. Sometimes that’s what you want; sometimes it’s a surprise.
What to learn next
Now that the basics of files and structure are covered, the next chunk of the roadmap is text processing — grep, sed, awk — the tools that make the shell a data processing environment.