How to Use the ‘ln’ Command in Linux
Learn how to use the 'ln' command in Linux for better file organization and system customization.
The ln
command in Linux, short for “link,” is a powerful tool that allows users to create links between files and directories. By creating these connections, it facilitates efficient file management and organization within the Linux operating system. Similar to commands like cp
for copying files, the ln
command provides a way to reference the same content from different locations without duplicating the data.
It’s commonly used for creating symbolic links, which can be handy for managing configurations, scripts, or shared resources. For those looking to streamline their workflow, the ln
command can be used alongside other commands such as chmod
to set permissions, or find
to locate files. Whether you’re a seasoned professional or a Linux beginner, understanding the ln
command opens up new possibilities in file management and system customization.
How to Use ln
1. Creating a Hard Link
Syntax: ln TARGET LINK_NAME
Explanation: Creates a hard link to a file.
Example: ln file1.txt link1.txt
Output:
$
No output is shown in the terminal, indicating success. A hard link named link1.txt
to the file file1.txt
has been created.
2. Creating a Symbolic Link
Syntax: ln -s TARGET LINK_NAME
Explanation: Creates a symbolic link to a file or directory.
Example: ln -s /path/to/original/file.txt symlink.txt
Output:
$
No output is shown in the terminal, indicating success. A symbolic link named symlink.txt
pointing to /path/to/original/file.txt
has been created.
3. Creating a Symbolic Link to a Directory
Syntax: ln -s TARGET_DIRECTORY LINK_NAME
Explanation: Creates a symbolic link to a directory.
Example: ln -s /path/to/original/directory linked_directory
Output:
$
No output is shown in the terminal, indicating success. A symbolic link named linked_directory
pointing to /path/to/original/directory
has been created.
4. Creating a Hard Link with Verbose Output
Syntax: ln -v TARGET LINK_NAME
Explanation: Creates a hard link to a file and displays a message describing the action.
Example: ln -v file2.txt link2.txt
Output:
$ ln -v file2.txt link2.txt 'link2.txt' -> 'file2.txt' $
The terminal output shows the message link2.txt' -> 'file2.txt
, indicating that a hard link named link2.txt
to the file file2.txt
has been created.
5. Creating a Hard Link in a Specific Directory
Syntax: ln TARGET DIRECTORY
Explanation: Creates a hard link to a file in a specific directory.
Example: ln file3.txt /path/to/directory
Output:
$
No output is shown in the terminal, indicating success. A hard link to file3.txt
has been created in the specified directory.
6. Creating Symbolic Links Interactively
Syntax: ln -si TARGET LINK_NAME
Explanation: Creates a symbolic link, prompting before overwriting existing files.
Example: ln -si /path/to/file4.txt symlink4.txt
Output:
$ ln -si /path/to/file4.txt symlink4.txt ln: replace 'symlink4.txt'? y $
The terminal prompts the user to confirm overwriting the existing symlink4.txt
. The user enters ‘y
‘, and the symbolic link is created.
7. Creating a Hard Link with a Backup of Existing Destination Files
Syntax: ln -b TARGET LINK_NAME
Explanation: Creates a hard link, making a backup of an existing destination file.
Example: ln -b file5.txt link5.txt
Output:
$
No output is shown in the terminal, indicating success. If link5.txt
already existed, a backup is created, and the hard link is made.
8. Creating a Symbolic Link with Custom Suffix for Backup
Syntax: ln -s -b --suffix=.backup TARGET LINK_NAME
Explanation: Creates a symbolic link, making a backup of an existing destination file with a custom suffix.
Example: ln -s -b --suffix=.backup /path/to/file6.txt symlink6.txt
Output:
$
No output is shown in the terminal, indicating success. If symlink6.txt
already existed, a backup with the suffix .backup
is created, and the symbolic link is made.
More Linux commands:
Directory Operations | rmdir · cd · pwd · exa · ls |
File Operations | cat · cp · dd · less · touch · ln · rename · more · head |
File System Operations | chown · mkfs · locate |
Networking | ping · curl · wget · iptables · mtr |
Search and Text Processing | find · grep · sed · whatis · ripgrep · fd · tldr |
System Information and Management | env · history · top · who · htop · glances · lsof |
User and Session Management | screen · su · sudo · open |