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
Link Types
| Type | Option | Description |
|---|
| Hard link | (default) | Another name for the same file |
| Symbolic link | -s | Pointer to the target path |
Examples
Create symbolic link
$ ln -s /var/log/syslog ~/syslog
$ ls -l ~/syslog
lrwxrwxrwx 1 greys greys 15 Jan 29 10:00 syslog -> /var/log/syslog
Create hard link
$ 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
Link to directory (symlink only)
$ ln -s /var/www/html ~/website
Force overwrite existing link
$ ln -sf /new/target existing_link
Create relative symlink
$ ln -sr ../shared/config.yml ./config.yml
Symbolic vs Hard Links
| Feature | Symbolic | Hard |
|---|
| Cross filesystems | Yes | No |
| Link to directories | Yes | No |
| Target can be deleted | Yes (breaks link) | File exists until all links removed |
| Shows target with ls -l | Yes | No (same inode) |
| Size | Small (stores path) | Same as original |
Identifying Links
# 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/
Backup with hard links (space efficient)
$ 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