ln – create links between files

ln command
ln command

ln creates links between files. Symbolic links (symlinks) are like shortcuts; hard links are additional names for the same file.

Synopsis

ln [OPTIONS] TARGET LINK_NAME
ln -s TARGET LINK_NAME
TypeOptionDescription
Hard link(default)Another name for the same file
Symbolic link-sPointer to the target path

Examples

$ ln -s /var/log/syslog ~/syslog
$ ls -l ~/syslog
lrwxrwxrwx 1 greys greys 15 Jan 29 10:00 syslog -> /var/log/syslog
$ ln original.txt copy.txt
$ ls -li original.txt copy.txt
12345 -rw-r--r-- 2 greys greys 100 Jan 29 10:00 copy.txt
12345 -rw-r--r-- 2 greys greys 100 Jan 29 10:00 original.txt
# Same inode number (12345) = same file
$ ln -s /var/www/html ~/website
$ ln -sf /new/target existing_link
$ ln -sr ../shared/config.yml ./config.yml
FeatureSymbolicHard
Cross filesystemsYesNo
Link to directoriesYesNo
Target can be deletedYes (breaks link)File exists until all links removed
Shows target with ls -lYesNo (same inode)
SizeSmall (stores path)Same as original
# Symlink
$ ls -l
lrwxrwxrwx 1 greys greys 15 Jan 29 10:00 link -> /path/to/target

# Hard link (link count > 1)
$ ls -l
-rw-r--r-- 2 greys greys 100 Jan 29 10:00 file

# Show what symlink points to
$ readlink link
/path/to/target

$ readlink -f link    # Full canonical path

Common Patterns

Version management

$ ln -s python3.11 /usr/bin/python
$ ln -sf python3.12 /usr/bin/python    # Update

Configuration alternatives

$ ln -s /etc/nginx/sites-available/mysite /etc/nginx/sites-enabled/
$ cp -al /backup/daily.1 /backup/daily.2

Tips

  • Use symlinks usually: More flexible, cross-filesystem
  • Careful with relative paths: Symlinks store the exact path given
  • Broken symlinks: Red in colored ls, still take up space
  • Hard links can’t cross filesystems: Use symlinks instead
  • Deleting original: Hard link survives, symlink breaks

See Also

  • ls — List files (shows links with -l)
  • readlink — Print symlink target
  • unlink — Remove link
  • cp — Copy files

Tutorials