ln Command
Create hard links and symbolic links between files and directories in Linux.
Syntax
ln [OPTION]... TARGET LINK_NAME
ln [OPTION]... TARGET... DIRECTORY
ln [OPTION]... -t DIRECTORY TARGET...
The ln command creates links between files. By default, it creates hard links, but with the -s option, it creates symbolic (soft) links.
Common Options
| Option |
Description |
-s, --symbolic |
Create symbolic links instead of hard links |
-f, --force |
Remove existing destination files |
-i, --interactive |
Prompt before removing existing files |
-n, --no-dereference |
Treat destination that is a symlink as normal file |
-r, --relative |
Create symbolic links relative to link location |
-t, --target-directory |
Specify target directory |
-v, --verbose |
Print name of each linked file |
--backup |
Make backup of existing destination files |
Basic Examples
Creating symbolic links
# Create symbolic link to a file
ln -s /path/to/original/file.txt mylink.txt
# Create symbolic link to a directory
ln -s /path/to/directory mydir
# Create symbolic link with verbose output
ln -sv /etc/passwd passwd-link
# Create relative symbolic link
ln -sr ../config/app.conf current-config
Create symbolic links (shortcuts) that point to other files or directories
Creating hard links
# Create hard link to a file
ln original.txt hardlink.txt
# Create hard link with verbose output
ln -v document.pdf backup-document.pdf
# Force creation (overwrite existing)
ln -f source.txt existing-link.txt
Create hard links that are additional names for the same file data
Multiple links
# Create multiple symbolic links in a directory
ln -s /usr/bin/python3 /usr/local/bin/python
ln -s /usr/bin/pip3 /usr/local/bin/pip
# Create links for multiple files
ln -st /backup/ file1.txt file2.txt file3.txt
# Create symbolic links for all files in directory
ln -s /source/dir/* /target/dir/
Create multiple links at once or link multiple files
Advanced Usage
Interactive and safe linking
# Prompt before overwriting existing files
ln -si /path/to/source existing-link
# Create backup of existing destination
ln -s --backup=numbered source.txt link.txt
# Force overwrite without prompting
ln -sf new-target.txt existing-link.txt
# Treat symlink destination as normal file
ln -sn new-target existing-symlink
Safe linking practices with prompts and backups
Working with directories
# Link to target directory
ln -st /target/directory/ file1 file2 file3
# Create symbolic link to directory
ln -s /var/log/myapp logs
# Create relative link to parent directory
ln -sr ../shared/config local-config
# Link all files from source to target directory
find /source -type f -exec ln -s {} /target/ \;
Advanced directory linking operations
Conditional linking
# Create link only if target doesn't exist
[ ! -e mylink ] && ln -s /path/to/target mylink
# Create link with error handling
ln -s /path/to/target mylink 2>/dev/null || echo "Link creation failed"
# Replace broken symbolic links
find . -type l ! -exec test -e {} \; -exec ln -sf /new/target {} \;
# Create temporary symbolic link
ln -s /tmp/data temp-link && trap 'rm -f temp-link' EXIT
Conditional and error-safe link creation
Hard Links vs Symbolic Links
Hard Links
- Multiple names for same file data
- Share same inode number
- Cannot cross filesystem boundaries
- Cannot link to directories
- File exists until all hard links removed
- No additional disk space used
Symbolic Links
- Separate file pointing to another file
- Different inode number
- Can cross filesystem boundaries
- Can link to directories
- Becomes broken if target is removed
- Uses small amount of disk space
Viewing link information
# View detailed file information
ls -l mylink
# Show inode numbers
ls -li original.txt hardlink.txt
# Follow symbolic links
ls -L symlink
# Find all hard links to a file
find . -samefile original.txt
# Check if file is a symbolic link
[ -L mylink ] && echo "It's a symbolic link"
# Get target of symbolic link
readlink mylink
readlink -f mylink # Follow all links
Commands to examine and verify links
Practical Examples
Configuration management
# Link configuration files
ln -s /etc/nginx/sites-available/mysite /etc/nginx/sites-enabled/
# Create development configuration links
ln -sf config/development.conf app.conf
# Link shared configuration
ln -s /shared/config/database.yml config/database.yml
# Version-specific links
ln -sf python3.9 /usr/bin/python3
Managing configuration files with symbolic links
Backup and versioning
# Create backup hard links
ln important.txt important.txt.backup
# Version management with links
ln -sf app-v2.1.0 current-app
# Snapshot with hard links (saves space)
cp -al /data/original/ /backup/snapshot-$(date +%Y%m%d)/
# Link to latest backup
ln -sfn backup-$(date +%Y%m%d) latest-backup
Using links for backup and version management
Development workflows
# Link development tools
ln -s /opt/nodejs/bin/node /usr/local/bin/
ln -s /opt/nodejs/bin/npm /usr/local/bin/
# Create project shortcuts
ln -s /var/www/html/myproject ~/project
# Link shared libraries
ln -s /usr/lib/x86_64-linux-gnu/libssl.so.1.1 libssl.so
# Environment-specific links
ln -sf .env.production .env
Development environment setup with symbolic links
Troubleshooting
Common Issues
- Broken symbolic links - Target file was moved or deleted
- Permission denied - Insufficient permissions to create link
- Cross-device link - Trying to create hard link across filesystems
- File exists - Destination already exists and -f not used
Finding and fixing broken links
# Find broken symbolic links
find . -type l ! -exec test -e {} \; -print
# Find and remove broken links
find . -type l ! -exec test -e {} \; -delete
# Fix broken link by updating target
ln -sfn /new/correct/path broken-link
# Check if link target exists
if [ ! -e "$(readlink mylink)" ]; then
echo "Link target does not exist"
fi
Identify and repair broken symbolic links
Link verification
# Verify hard link relationship
stat file1 file2 | grep Inode
# Check symbolic link target
readlink -e symlink # Canonical path
# Test link accessibility
[ -r mylink ] && echo "Link is readable"
# Count hard links to file
stat -c %h filename
Verify link integrity and relationships