Overview
ln creates links between files, allowing multiple filenames to reference the same data. Linux supports two types of links: hard links (multiple directory entries pointing to the same inode) and symbolic links or symlinks (special files containing paths to other files). Understanding links is fundamental to Unix filesystem concepts and essential for efficient system administration, package management, and file organization.
Hard Links vs Symbolic Links
| Feature | Hard Link | Symbolic Link (Symlink) |
|---|---|---|
| Command | ln source target | ln -s source target |
| What it is | Additional directory entry for same inode | Special file containing path to target |
| Inode | Same inode as original file | Different inode (its own) |
| Cross filesystem | No - must be same filesystem | Yes - can link across filesystems |
| Link to directories | No (except for system) | Yes |
| Original deleted | File remains accessible via other links | Link becomes broken/dangling |
| Disk space | No additional space (same data) | Tiny (just stores path string) |
| Permissions | Always same as original (shared inode) | Own permissions (usually 777, actual access controlled by target) |
| Relative paths | N/A | Can use relative or absolute paths |
| Link count | Increments original's link count | Doesn't affect original's link count |
| Find original | find -inum [inode] | readlink -f symlink |
┌──────────────┐
│ Inode 123 │ ← Single inode containing actual file data
│ (file data) │
└──────────────┘
↑ ↑
│ │
file1.txt file2.txt ← Two directory entries pointing to same inode
Symbolic Link Concept:
┌──────────────┐ ┌──────────────┐
│ Inode 123 │ ←────│ Inode 456 │
│ (file data) │ │ (path string)│
│ file1.txt │ │ → file1.txt │
└──────────────┘ └──────────────┘
↑
symlink
Example 1: Create a Symbolic Link (Most Common)
$ ln -s /path/to/original /path/to/link$ ln -s /usr/bin/python3.9 /usr/bin/python$ ln -s ~/Documents/project ~/Desktop/project$ ln -s /var/www/html/app /var/www/current
The -s flag creates a symbolic link (symlink). This is the most commonly used form of ln because symlinks are more flexible.
Common use cases:
- Version management: Link "python" to "python3.9" - easy to switch versions
- Shortcuts: Create desktop shortcuts to deeply nested directories
- Deployment: Link "current" to specific version directory for easy rollbacks
- Configuration: Link config files to central locations
- Convenience: Shorter paths to frequently accessed files
Example 2: Create a Hard Link
$ ln original.txt hardlink.txt$ ln /home/user/data.db /backup/data.db# Verify both point to same inode:
$ ls -li original.txt hardlink.txt
1234567 -rw-r--r-- 2 user user 1024 Nov 7 10:00 original.txt
1234567 -rw-r--r-- 2 user user 1024 Nov 7 10:00 hardlink.txt
Hard links create additional directory entries for the same inode. Both filenames are equal - neither is more "original" than the other.
When to use hard links:
- Backup safety: Accidental deletion of one name doesn't lose data
- Space efficiency: Multiple "copies" with no extra disk space
- Atomic updates: Update one link, all others instantly reflect changes
- Deduplication: File system deduplication often uses hard links
Link count explanation: The "2" in the ls output (second column) shows this inode has 2 links (2 directory entries pointing to it). When link count reaches 0, the file is deleted.
Example 3: Create Symbolic Link with Relative Path
$ cd /var/www/html$ ln -s app-v2.1 current # Relative path$ ls -l current
lrwxrwxrwx 1 user user 8 Nov 7 10:00 current -> app-v2.1# To update to new version:
$ ln -sfn app-v2.2 current # Force, no-dereference
Relative symlinks store the path relative to the link's location. This is useful for:
- Portable directory structures: Moving the entire directory tree keeps links valid
- Version switching: Common in web deployments for blue-green deployment
- Simplicity: Shorter stored paths in the symlink
The link "current" contains just "app-v2.1" (relative), not "/var/www/html/app-v2.1" (absolute).