Links in Linux create references to files, but hard links and soft links (symlinks) work fundamentally differently. Understanding the distinction helps you make the right choice for configuration management, deployments, and backup strategies.
How Hard Links Work
A hard link is an additional directory entry that points to the same inode (data blocks) on disk. The file data is shared — there is no original or copy, just multiple names for the same data.
# Create a hard link
ln original.txt hardlink.txt
# Both files point to the same inode
ls -li original.txt hardlink.txt
# 1234567 -rw-r--r-- 2 user user 100 Jan 15 original.txt
# 1234567 -rw-r--r-- 2 user user 100 Jan 15 hardlink.txt
# Same inode (1234567), link count is 2
# Modifying one modifies the other (same data)
echo "new content" >> hardlink.txt
cat original.txt # Shows "new content" too
# Deleting one does NOT delete the data
rm original.txt
cat hardlink.txt # Still works! Data exists until link count reaches 0
How Soft Links (Symlinks) Work
A symbolic link is a special file that contains the path to another file. It is essentially a pointer or shortcut.
# Create a symbolic link
ln -s /var/www/myapp/current /var/www/html
# The symlink shows where it points
ls -la /var/www/html
# lrwxrwxrwx 1 root root 24 Jan 15 /var/www/html -> /var/www/myapp/current
# If the target is deleted, the symlink becomes broken (dangling)
rm /var/www/myapp/current
cat /var/www/html # Error: No such file or directory
# Find broken symlinks
find /var/www -xtype l
Key Differences
| Feature | Hard Link | Soft Link |
|---|---|---|
| Cross filesystem | No | Yes |
| Link to directories | No (usually) | Yes |
| Survives target deletion | Yes | No (becomes dangling) |
| Own inode | No (shares inode) | Yes (separate inode) |
| File size | Same as original | Length of path string |
| Relative paths | N/A | Supported |
When to Use Hard Links
- Backups — Incremental backups (rsnapshot) use hard links for unchanged files, saving disk space
- Log file rotation — Some tools use hard links during rotation
- When you need resilience — The data persists even if one name is deleted
When to Use Soft Links
- Deployment symlinks — Point current to the latest release directory
- Configuration management — Link config files to a central location
- Cross-filesystem references — Link between different partitions
- Directory links — Only symlinks can link directories
Practical Examples
Zero-Downtime Deployment with Symlinks
# Release directory structure
/var/www/myapp/
releases/
20250115-120000/
20250116-093000/
current -> releases/20250116-093000/
# Deploy new release
cd /var/www/myapp
tar xzf release.tar.gz -C releases/20250117-140000/
# Atomic switch (ln -sfn is atomic on most filesystems)
ln -sfn releases/20250117-140000 current
# Rollback is instant
ln -sfn releases/20250116-093000 current
Nginx Configuration
# Enable a site by creating a symlink
sudo ln -s /etc/nginx/sites-available/myapp.conf /etc/nginx/sites-enabled/
# Disable a site by removing the symlink
sudo rm /etc/nginx/sites-enabled/myapp.conf
# The original file in sites-available is untouched
Finding and Managing Links
# Find all symlinks in a directory
find /etc -type l
# Find all broken symlinks
find / -xtype l 2>/dev/null
# Find all hard links to a file (by inode)
find / -inum $(stat -c %i /path/to/file) 2>/dev/null
# Check if a file is a symlink
test -L /var/www/html && echo "symlink" || echo "not a symlink"