principlebashModerate
Symlinks vs Hardlinks: Choosing the Right One
Viewed 0 times
symlinkhardlinkinodeln -sreadlinkdangling linkfilesystem
linux
Error Messages
Problem
Developers use symlinks and hardlinks interchangeably without understanding the constraints, leading to broken links after moves or across filesystems.
Solution
Use symlinks for cross-filesystem links or directories. Use hardlinks for same-filesystem file deduplication.
# Symlink (soft link) — can cross filesystems, can link directories
ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/myapp
# Check symlink target
readlink -f /etc/nginx/sites-enabled/myapp
# Hardlink — same filesystem only, shares inode
ln /var/data/large-file.bin /var/backup/large-file.bin
# Find all hardlinks sharing an inode
find /var -inum $(stat -c '%i' /var/data/large-file.bin)
# Find broken symlinks
find /etc/nginx/sites-enabled -xtype lWhy
Hardlinks share an inode — deleting one does not remove the data until all links are gone. Symlinks are just path pointers — if the target moves, the symlink breaks. Hardlinks cannot span filesystems or link to directories.
Gotchas
- Symlinks can create circular references causing infinite loops in tools that follow them.
- Copying a directory with
cp -rdoes not preserve hardlinks — usecp -aorrsync -aH. - A dangling symlink exists as a file but its target is missing —
ls -lashows it, butcatfails. - NFS and some virtual filesystems do not support hardlinks.
Revisions (0)
No revisions yet.