ln ‐ #files - five4nets/Linux-Knowledgebase GitHub Wiki
ln
Command in Linux
Tutorial: Using the The ln
command in Linux creates links between files or directories, allowing multiple references to the same data. This tutorial explains the ln
command, its options, and provides practical examples for creating hard and symbolic links.
ln
Command?
What is the The ln
(link) command creates either hard links or symbolic links (symlinks):
- Hard Link: A direct reference to the same inode as the original file. Changes to the hard link or original file affect both.
- Symbolic Link: A pointer to another file or directory. It acts like a shortcut and can point to non-existent files or across filesystems.
Syntax:
ln [options] SOURCE [TARGET]
SOURCE
: The file or directory to link to.TARGET
: The name of the link (optional; defaults to the source name in the current directory).
Common Options
Option | Description |
---|---|
-s , --symbolic |
Creates a symbolic link (default is a hard link). |
-f , --force |
Overwrites existing target files without prompting. |
-i , --interactive |
Prompts before overwriting existing files. |
-v , --verbose |
Displays the names of files as links are created. |
-n , --no-dereference |
Treats a symlink to a directory as a file, not a directory. |
-b , --backup |
Creates a backup of the target file before overwriting. |
Key Concepts
- Inodes: Hard links share the same inode, so they reference the same data on disk. Deleting one link doesn’t affect others until all links are removed.
- Symlinks: Symbolic links are independent files pointing to the source. If the source is deleted, the symlink becomes broken.
- Permissions: Hard links inherit the source file’s permissions. Symlinks have their own permissions (usually
lrwxrwxrwx
).
Examples
1. Creating a Hard Link
Create a hard link named file1_hardlink.txt
for file1.txt
:
ln file1.txt file1_hardlink.txt
- Both
file1.txt
andfile1_hardlink.txt
point to the same data. - Verify with
ls -li
to see identical inode numbers:
ls -li
Output (example):
12345 -rw-r--r-- 2 user user 0 Jun 25 14:00 file1.txt
12345 -rw-r--r-- 2 user user 0 Jun 25 14:00 file1_hardlink.txt
The 2
indicates two hard links to the same inode (12345
).
2. Creating a Symbolic Link
Create a symbolic link named file1_symlink.txt
for file1.txt
:
ln -s file1.txt file1_symlink.txt
- Verify with
ls -l
:
ls -l
Output:
lrwxrwxrwx 1 user user 9 Jun 25 14:05 file1_symlink.txt -> file1.txt
-rw-r--r-- 1 user user 0 Jun 25 14:00 file1.txt
The arrow (->
) shows the symlink points to file1.txt
.
3. Forcing Overwrite of an Existing Link
If file1_symlink.txt
already exists, overwrite it:
ln -sf file2.txt file1_symlink.txt
- The
-f
flag forces the overwrite, and-s
ensures it’s a symbolic link. - Check the updated link:
ls -l file1_symlink.txt
Output:
lrwxrwxrwx 1 user user 9 Jun 25 14:10 file1_symlink.txt -> file2.txt
4. Creating a Symbolic Link to a Directory
Link to a directory named docs
:
ln -s /home/user/docs docs_link
- The symlink
docs_link
points to/home/user/docs
. - Verify:
ls -l
Output:
lrwxrwxrwx 1 user user 14 Jun 25 14:15 docs_link -> /home/user/docs
5. Creating Multiple Hard Links
Create hard links for data.txt
in multiple directories:
ln data.txt /home/user/backup/data_link1.txt
ln data.txt /home/user/archive/data_link2.txt
- Both links share the same inode as
data.txt
. - Verify inode numbers with
ls -li
.
6. Verbose Mode
Use -v
to see link creation details:
ln -sv file3.txt file3_link.txt
Output:
'file3_link.txt' -> 'file3.txt'
7. Avoiding Directory Symlink Dereferencing
If dir_link
is a symlink to a directory, create a symlink to dir_link
itself (not the directory it points to):
ln -sn dir_link new_dir_link
- Without
-n
,ln
would create a link inside the target directory.
Practical Use Cases
- Backups: Use hard links to save disk space while maintaining multiple references to a file.
- Version Control: Symbolic links can point to the latest version of a file or directory.
- Software Management: Symlinks are used to point to executable files in
/usr/bin
.
Caveats
- Hard Links: Cannot span different filesystems or point to directories (on most systems).
- Symbolic Links: Can become broken if the source is moved or deleted.
- Permissions: Ensure you have write permissions in the directory where the link is created.