ln
Overview
The ln
command creates links between files. It can create both hard links and symbolic (soft) links, providing different ways to reference files in the filesystem.
Syntax
ln [options] target [link_name]
ln [options] target... directory
Common Options
Option | Description |
---|---|
-s |
Create symbolic link |
-f |
Force creation |
-i |
Interactive mode |
-v |
Verbose output |
-b |
Backup existing files |
-n |
No dereference |
-r |
Relative symbolic links |
-t directory |
Target directory |
Link Types
Type | Description |
---|---|
Hard Link | Direct reference to inode |
Symbolic Link | Pointer to file path |
Relative Link | Path relative to link location |
Absolute Link | Full path reference |
Key Use Cases
- Create file shortcuts
- Share files across directories
- Version management
- Space-efficient duplicates
- Configuration management
Examples with Explanations
Example 1: Create Symbolic Link
ln -s /path/to/file link_name
Creates a symbolic link pointing to the target file
Example 2: Create Hard Link
ln file.txt hardlink.txt
Creates a hard link to the same inode
Example 3: Link to Directory
ln -s /usr/local/bin ~/bin
Creates symbolic link to directory
Understanding Links
Hard links: - Share same inode - Cannot cross filesystems - Cannot link directories - Survive original deletion
Symbolic links: - Point to path string - Can cross filesystems - Can link directories - Break if target deleted
Common Usage Patterns
Create backup link:
ln -s config.conf config.conf.bak
Multiple links:
ln -s target link1 link2 link3
Force overwrite:
ln -sf new_target existing_link
Performance Analysis
- Hard links have no performance overhead
- Symbolic links require extra filesystem lookup
- Use hard links for performance-critical scenarios
- Symbolic links more flexible for cross-filesystem usage
Additional Resources
Best Practices
- Use absolute paths for system links
- Use relative paths for portable links
- Document link purposes
- Check link validity regularly
- Avoid circular symbolic links
Troubleshooting
- Broken symbolic links
- Permission issues
- Cross-filesystem limitations
- Circular references
- Link target changes