Overview
The ln command creates references (links) to existing files or directories. This allows access to the same data from multiple locations without copying the original data. Symbolic links, in particular, act as pointers to the original file's path, and the link becomes broken if the original file is deleted.
Link Types
The two main types of links that can be created with the ln command.
- Hard Link: Another name for a file that shares the same inode. The data remains even if the original file is deleted. Cannot be created across different file systems, and cannot be created for directories.
- Symbolic Link (Soft Link): A pointer file that points to the path of the original file. The link breaks if the original file is deleted. Can be created across different file systems and for directories.
Key Options
The ln command is primarily used to create symbolic links, but various options can control the behavior of the links.
Link Types
Behavior Control
Generated command:
Try combining the commands.
Description:
`ln` Executes the command.
Combine the above options to virtually execute commands with AI.
Usage Examples
Examples of creating various types of links using the ln command.
Create a Basic Symbolic Link
ln -s /path/to/original_file /path/to/symlink
Creates a symbolic link to an original file.
Create a Symbolic Link in the Current Directory (Relative Path)
ln -s ../../data/my_data.txt my_data_link.txt
Creates a symbolic link using a relative path in the current working directory. (Caution: Relative paths can be prone to breaking.)
Create a Symbolic Link to a Directory
ln -s /var/www/html /home/user/web_root
Creates a symbolic link to a directory.
Forcefully Overwrite an Existing Link
ln -sf /new/path/to/file /path/to/existing_link
If a link already exists at the target location, overwrite it with the new link without confirmation.
Create a Hard Link
ln /path/to/original_file /path/to/hardlink
Creates a hard link to an original file. (Not possible for directories)
Tips & Precautions
Points to note and tips for efficient use of the ln command.
Symbolic Link Paths
Precautions regarding specifying the original file's path when creating symbolic links.
- It is recommended to use absolute paths when creating symbolic links. Relative paths can easily lead to broken links.
- Example: `ln -s /absolute/path/to/file link_name` (Recommended)
- Example: `ln -s ../file link_name` (Behavior may vary depending on where the link is created, so caution is advised)
Hard Link Limitations
Limitations to be aware of when using hard links.
- Hard links can only be created within the same file system.
- Hard links cannot be created for directories.
Behavior When Original File is Deleted
How links behave when the original file is deleted.
- If the original file of a symbolic link is deleted, the symbolic link becomes a 'broken link' and can no longer point to the original file.
- Even if the original file of a hard link is deleted, the data remains, and the data will not be removed from the file system as long as other hard links exist.