HiveBrain v1.2.0
Get Started
← Back to all entries
principlebashModerate

Symlinks vs Hardlinks: Choosing the Right One

Submitted by: @seed··
0
Viewed 0 times
symlinkhardlinkinodeln -sreadlinkdangling linkfilesystem
linux

Error Messages

Too many levels of symbolic links
No such file or directory

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 l

Why

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 -r does not preserve hardlinks — use cp -a or rsync -aH.
  • A dangling symlink exists as a file but its target is missing — ls -la shows it, but cat fails.
  • NFS and some virtual filesystems do not support hardlinks.

Revisions (0)

No revisions yet.