The ln command is used to create links between files in Linux. It can create both hard links and symbolic (soft) links, which are essential for file system management and organization.
ln [options] target linkname
ln [options] target... directory
- Point directly to the file's inode
- Cannot span across different filesystems
- Cannot link to directories
- If original file is deleted, hard link still contains the data
- Point to the file path (like shortcuts)
- Can span across different filesystems
- Can link to directories
- If original file is deleted, symbolic link becomes broken
Some popular option flags include:
-s Create symbolic (soft) links instead of hard links
-f Force creation by removing existing destination files
-v Verbose output, show what's being linked
-n Treat destination as normal file if it's a symlink to a directory
-r Create relative symbolic links
-t Specify target directory for links
- Create a hard link
ln file.txt hardlink.txt- Create a symbolic link
ln -s /path/to/original/file.txt symlink.txt- Create a symbolic link to a directory
ln -s /var/log logs- Create multiple symbolic links in a directory
ln -s /usr/bin/python3 /usr/bin/gcc /usr/local/bin/- Create a relative symbolic link
ln -sr ../config/app.conf current_config- Force create a link (overwrite existing)
ln -sf /new/target existing_link- Create links with verbose output
ln -sv /source/file /destination/link- Creating shortcuts to frequently used files or directories
- Maintaining multiple versions of configuration files
- Organizing files without duplicating storage space
- Creating backup references to important files
- Setting up development environments with shared libraries
- Use
ls -lto see if a file is a symbolic link (indicated by->) - Use
ls -ito see inode numbers for hard links - Be careful with symbolic links to avoid creating circular references
- Hard links share the same inode and disk space
- Symbolic links take minimal disk space (just the path information)
The ln command is essential for efficient file system organization and is widely used in system administration and development workflows.
For more details, check the manual: man ln