Skip to content

Understanding Symbolic Links in Linux

Banner for Understanding Symbolic Links in Linux

A symbolic link (or symlink) is a special type of file that acts as a pointer to another file or directory. Unlike copying a file, a symlink is just a reference — the original data lives in one place, and the symlink is a shortcut to it.

Terminal window
ls -la /usr/local/bin/python
# lrwxrwxrwx 1 root root 7 Jan 15 10:00 /usr/local/bin/python -> python3

The -> arrow in ls output tells you a file is a symlink and shows its target.

Use the ln command with the -s flag to create a symlink:

Terminal window
# ln -s <target> <link-name>
ln -s /path/to/original /path/to/link

Examples:

Terminal window
# Link a config file into your home directory
ln -s /etc/nginx/nginx.conf ~/nginx.conf
# Link a binary into your PATH
ln -s /opt/my-tool/bin/tool /usr/local/bin/tool
# Link a directory
ln -s /var/www/html/current /var/www/html/latest
FeatureSymbolic LinkHard Link
Cross-filesystemYesNo
Points toPathInode
Breaks if target deletedYesNo
Works with directoriesYesNo (usually)
Shows in ls -laYes (->)No

When to use a symlink:

  • Pointing across filesystems
  • Linking to directories
  • Creating human-friendly aliases for versioned paths (e.g. python -> python3.12)

When to use a hard link:

  • Creating a true copy-on-write backup that survives the original being deleted
  • Saving disk space with duplicate files on the same filesystem
Terminal window
# -L returns true if path is a symlink
if [ -L /usr/local/bin/python ]; then
echo "It's a symlink"
fi
# Find the real path a symlink resolves to
readlink -f /usr/local/bin/python

A symlink becomes broken (or dangling) when its target no longer exists:

Terminal window
ln -s /tmp/does-not-exist /tmp/broken-link
ls -la /tmp/broken-link # shows the link, but target is missing
cat /tmp/broken-link # No such file or directory

Find all broken symlinks in a directory:

Terminal window
find /var/www -xtype l

You cannot overwrite a symlink with ln -s — it errors if the link already exists. Use -f (force) to replace it atomically:

Terminal window
ln -sf /opt/my-tool-2.0/bin/tool /usr/local/bin/tool

The -f flag makes the swap atomic, which is important for services checking the link at runtime.

Real-World Use Cases

1. Managing multiple Python versions:

Terminal window
ln -sf /usr/bin/python3.12 /usr/local/bin/python
ln -sf /usr/bin/python3.12 /usr/local/bin/python3

2. Zero-downtime deployments:

Terminal window
# New release lands in /var/www/releases/v2
ln -sf /var/www/releases/v2 /var/www/current
# Nginx serves /var/www/current — now points to v2

3. Dotfile management:

Terminal window
# Keep your config in a Git repo, symlink into place
ln -s ~/dotfiles/.bashrc ~/.bashrc
ln -s ~/dotfiles/.vimrc ~/.vimrc

Summary

Symbolic links are one of Unix’s most useful primitives. They let you:

  • Create aliases without duplicating data
  • Abstract over paths so deployments and upgrades become a single ln -sf
  • Build dotfile management systems backed by Git

Mastering symlinks is a small investment with large returns in shell scripting and system administration.