Overview of ln
Links create references to the original files or directories, allowing users to access the same data from multiple locations. This saves disk space, makes file management more flexible, and is useful when specific paths need to be referenced consistently in script writing.
Main Roles of Links
`ln` command is primarily used for the following purposes:
Key Use Cases
- Saving Disk Space: Instead of creating multiple copies of the same file, links are used to save space.
- Improving File Accessibility: Allows access from various paths regardless of the location of the original file.
- Version Control and Deployment: Sets links to refer to specific versions of library or configuration files.
- Shell Scripting: Used when creating dynamic path handling or easy references to specific files.
Hard Links vs Symbolic Links
`ln` command can create two main types of links that have important differences in how they operate and their characteristics.
Link Type Comparison
- **Hard Link**: Another name (directory entry) for the actual data of the file. The original file and the hard link point to the same inode (actual data block). If the original file is deleted, the data remains as long as the hard link exists. However, it can only be created within the same file system and cannot be created for directories.
- **Symbolic Link (Soft Link)**: A small file that contains the path to the original file or directory. It acts as a pointer that refers to the original file itself. If the original file is deleted, the symbolic link becomes broken and is no longer valid. It can be created across different file systems and can also be created for directories.
Key ln Command Options
`ln` command by default creates hard links, and the `-s` option can be used to create symbolic links.
1. Creating Links
2. Other Useful Options
Generated command:
Try combining the commands.
Description:
`ln` Executes the command.
Combine the above options to virtually execute commands with AI.
Usage Examples
Learn how to create and manage hard links and symbolic links through various examples of using the `ln` command.
Create Hard Link to a File
echo "Hello, Hard Link!" > original.txt
ln original.txt hardlink.txt
Creates a hard link named `hardlink.txt` to the `original.txt` file. Both files now share the same data.
Create Symbolic Link to a File
echo "Symbolic Link Test" > source_file.txt
ln -s source_file.txt symlink_to_file.txt
Creates a symbolic link named `symlink_to_file.txt` to the `source_file.txt` file. The symbolic link acts as a pointer to the original file.
Create Symbolic Link to a Directory
ln -s /var/log/apache2 apache_logs
Creates a symbolic link named `apache_logs` to the `/var/log/apache2` directory in the current directory. This allows access to the directory with a shorter name instead of a long path.
Force Overwrite When Creating Symbolic Link
ln -sf new_target.txt existing_link
Even if a link named `existing_link` already exists, forcibly creates a symbolic link pointing to `new_target.txt` without warning.
Verbose Output When Creating Link
ln -sv report.pdf daily_report.pdf
Creates a symbolic link `daily_report.pdf` to the `report.pdf` file while outputting detailed information about the creation process.
Check Link Status (ls -l)
ls -l
Use the `ls -l` command to check the detailed information of the file and link. The symbolic link points to the original file with `->`.