In Linux, the ln
command is used to create links between files. Links can be symbolic (soft) links or hard links, each with unique properties and use cases.
- A symbolic link is a special type of file that points to another file or directory. It acts as a shortcut and contains a reference to the target file or directory.
- Symbolic links can span across different filesystems and partitions.
-
Command:
ln -s [TARGET] [LINK_NAME]
-
Example:
ln -s /path/to/original/file.txt /path/to/link/file_link.txt
- Creates a symbolic link named
file_link.txt
pointing tofile.txt
.
- Creates a symbolic link named
-
Symbolic links can point to directories.
-
They break if the target file or directory is moved or deleted.
-
Easily identifiable by an arrow (
->
) when usingls -l
:lrwxrwxrwx 1 user user 11 Aug 2 12:34 file_link.txt -> file.txt
-
Can be used to link across different filesystems.
- Creating shortcuts to frequently used files or directories.
- Linking libraries or configuration files without duplicating content.
- Managing versioned files and directories by pointing a fixed name link to different versions.
- A hard link is a direct reference to the data of a file. Both the original file and the hard link point to the same inode, making them indistinguishable.
- Hard links share the same data blocks on the disk and only exist within the same filesystem.
-
Command:
ln [TARGET] [LINK_NAME]
-
Example:
ln /path/to/original/file.txt /path/to/link/file_link.txt
- Creates a hard link named
file_link.txt
pointing tofile.txt
.
- Creates a hard link named
-
Hard links cannot point to directories.
-
They do not break if the original file is moved or deleted, as they share the same data.
-
Identical file properties (same inode) and appear as normal files in
ls -l
:-rw-r--r-- 2 user user 1234 Aug 2 12:34 file.txt -rw-r--r-- 2 user user 1234 Aug 2 12:34 file_link.txt
-
Cannot link across different filesystems.
- Ensuring redundancy and backup without extra disk space usage.
- Sharing large files between users without duplicating the data.
- Implementing file version control where the content remains unchanged.
Here's a quick comparison between symbolic links and hard links:
Feature | Symbolic Links | Hard Links |
---|---|---|
Creation Command | ln -s [TARGET] [LINK_NAME] |
ln [TARGET] [LINK_NAME] |
Target Type | Files or directories | Files only |
File System | Can span across filesystems | Same filesystem only |
Inode | Separate inode from the target | Same inode as the target |
Link Breaking | Breaks if target is deleted/moved | Remains valid unless inode is deleted |
Usage | Shortcuts, configuration files | Backup, file sharing |
ln -s /usr/local/bin/python3 /usr/bin/python
- Creates a symbolic link named
python
pointing topython3
.
ln /home/user/file.txt /home/user/file_link.txt
- Creates a hard link named
file_link.txt
pointing tofile.txt
.
ls -li
- Displays files with inode numbers and link information.
-
Remove Symbolic Link:
rm /path/to/symbolic_link
-
Remove Hard Link:
rm /path/to/hard_link
-
Removing a symbolic or hard link does not affect the target file itself.
To check if a symbolic link is correctly pointing to its target:
ls -l /path/to/link
- Output shows the link with an arrow (
->
) pointing to the target.
To find files sharing the same inode (i.e., hard links):
ls -li /path/to/directory
- Compare the inode numbers. Files with the same inode are hard links to the same data.
123456 -rw-r--r-- 2 user user 1234 Aug 2 12:34 original_file.txt
123456 -rw-r--r-- 2 user user 1234 Aug 2 12:34 hard_link.txt
-
Quickly switch between different configuration files:
ln -s ~/config/config_v1.cfg ~/app/config.cfg
- Allows easy updates to
config.cfg
by pointing to different versions.
- Allows easy updates to
-
Create a backup without extra space:
ln /data/important.doc /backup/important.doc
- Ensures both files point to the same data, conserving disk space.
-
Symbolically link shared libraries in development environments:
ln -s /usr/lib/libXYZ.so /usr/local/lib/libXYZ.so
- Simplifies access and organization of shared resources.
- Symbolic Links (Soft Links): Best for shortcuts, spanning filesystems, and linking directories. Use the
ln -s
command. - Hard Links: Ideal for data backup, redundancy, and ensuring multiple access points to the same file data. Use the
ln
command.
By understanding these differences and usage scenarios, you can effectively manage files and directories in your Linux environment, leveraging the power of symbolic and hard links. This cheatsheet serves as a quick reference for using the ln
command efficiently.