Linux ln Command
Linux ln
Command
The ln
command in Linux is used to create links between files. A link acts as a shortcut or reference to another file. Using links, you can access the same file content using different names or locations.
Types of Links
There are two types of links the ln command can create.
- Hard Link
- Soft Link ( Symbolic Link )
Hard Link (Default)
The hard link points directly to the file’s data on disk (inode).
- The original file and the hard link share the same inode number.
- If the original file is deleted, the data remains as long as at least one hard link exists.
- Cannot be created for directories or across different file systems.
Syntax:
$ ln source_file link_name
Example:
$ ln file1.txt file1_hardlink.txt
This creates a hard link named file1_hardlink.txt
pointing to file1.txt
. Editing one affects the other.
Symbolic Link (Soft Link)
The soft link is a shortcut or pointer to the original file path.
- If the original file is deleted, the symlink becomes broken (dangling).
- Can link directories and cross different file systems.
Syntax:
$ ln -s source_file link_name
Examples:
$ ln -s /home/user/file1.txt myshortcut.txt
This creates a symbolic link named myshortcut.txt
pointing to file1.txt
.
Create symbolic link to directory
$ ln -s /home/testingdocs/Documents docs
Useful Options
Option | Description |
---|---|
-s | Create a symbolic (soft) link |
-v | Verbose output, shows what is happening |
-f | Force link creation (overwrite existing link) |
-i | Prompt before overwriting existing files |
Example:
$ ln -sv /home/user/docs report_link
This creates a symbolic link with verbose output.
Check Links
Use the ls -l
command to verify:
$ ls -l
- Hard link → shows same inode (no arrow).
- Symbolic link → shows an arrow (
→
) pointing to the target.
Type | Inode Shared | Cross File System | If Original Deleted |
---|---|---|---|
Hard Link | Yes | No | Data safe |
Symbolic Link | No | Yes | Link broken |