In Linux systems, a “link” is a special type of file that allows a file to have multiple names or paths within the file system. Links are a key concept for understanding the structure and management of files in Linux. They are mainly divided into two types:Hard Links and Symbolic Links (also known as Soft Links).
Below, we will detail the principles, characteristics, creation methods, and differences between these two types of links.
1. Hard Links
1. What is a Hard Link?
A hard link is a link where multiple file names point to the same inode in the file system. In Linux, each file has a unique inode that stores the file’s metadata (such as permissions, size, timestamps) and pointers to the actual data blocks.
When a hard link is created, it actually creates a new directory entry that points to the same inode as the original file. Therefore, hard links and the original file are essentially “the same file” with different names.
2. Characteristics
-
Shared inode: Hard links share the same inode as the original file, so they have the same inode number.
-
Deleting the original file does not affect hard links: As long as there is at least one hard link, the file’s data will not be deleted. The data is only truly released when all links pointing to that inode are deleted and no processes are using the file.
-
Cannot span file systems: Because inode numbers are independent across different file systems, hard links cannot span different file systems (e.g., from
<span>/dev/sda1</span>to<span>/dev/sda2</span>). -
Cannot link directories: To prevent circular references and confusion in the file system structure, most Linux systems do not allow hard links to be created for directories (only the root user can do this in rare cases, but it is not recommended).
-
Synchronized updates: Modifications to any hard link will reflect in all other links, as they operate on the same inode.
3. Creating Hard Links
Use the <span>ln</span> command (without the <span>-s</span> option):
ln source_file hard_link_name
Example:
ln file.txt hardlink_to_file.txt
# Create source file
echo "Hello World" > original.txt
# Create hard link
ln original.txt hardlink.txt
# Verify
ls -li original.txt hardlink.txt
# Output: same inode, link count is 2
# Deleting hard link does not affect original.txt
rm hardlink.txt
4. Viewing Hard Link Count
Use the <span>ls -l</span> command; the number in the second column indicates the hard link count for that inode:
ls -l file.txt
# Example output: -rw-r--r-- 2 user user 1234 Jan 1 10:00 file.txt
# Here, "2" indicates that this file has 2 hard links (including itself)
2. Symbolic Links (Soft Links)
1. What is a Symbolic Link?
A symbolic link is an independent file whose content is the pathname of another file or directory. It is similar to a “shortcut” in Windows. Symbolic links have their own inode and data blocks, which store the path of the target file.
2. Characteristics
-
Independent inode: Symbolic links have their own independent inode, different from the target file.
-
Can span file systems: Because symbolic links store pathnames, they can point to files in different file systems.
-
Can link directories: Symbolic links can point to files or directories.
-
Invalid if target file is deleted: If the target file is deleted, the symbolic link becomes a “dangling link” and will report an error when accessed: “No such file or directory”.
-
Occupies additional space: A symbolic link itself is a file and will occupy a small amount of disk space (usually one data block).
3. Creating Symbolic Links
Use the <span>ln -s</span> command:
ln -s target_file symbolic_link_name
Example:
# Create symbolic link (basic usage)
ln -s /home/user/documents/file.txt mylink.txt
# Link a large storage mount point to user directory
ln -s /mnt/data/Documents ~/Documents
# Create desktop shortcuts for commonly used directories
ln -s /var/www/html ~/Desktop/webroot
ln -s /home/user/projects ~/Desktop/myprojects
# Deleting symbolic link does not affect target
rm softlink.txt
4. Viewing Symbolic Links
Use the <span>ls -l</span> command; symbolic links will start with <span>l</span> and show the target they point to:
ls -l mylink.txt
# Example output: lrwxrwxrwx 1 user user 23 Jan 1 10:05 mylink.txt -> /home/user/documents/file.txt
3. Hard Links vs Symbolic Links: Comparative Summary
| Feature | Hard Link | Symbolic Link |
|---|---|---|
| Shares inode | Yes | No (independent inode) |
| Can span file systems | No | Yes |
| Can link directories | No (usually) | Yes |
| Available after original file deletion | Yes (as long as the link exists) | No (becomes dangling link) |
| Occupies additional disk space | No (does not increase data blocks) | Yes (occupies a small amount of space) |
| Creation command | <span>ln source link</span> |
<span>ln -s target link</span> |
<span>ls -l</span> display |
Regular file, hard link count increases | Starts with <span>l</span>, shows <span>-> target</span> |
4. Practical Application Scenarios
-
Uses of Hard Links:
-
Backing up important files to ensure that even if the original file is accidentally deleted, a copy remains available.
-
Providing quick access to the same file in different directories, saving space.
Uses of Symbolic Links:
-
Creating shortcuts for commonly used directories (e.g.,
<span>~/desktop/code -> /var/www/html</span>). -
Version management (e.g.,
<span>python -> python3.9</span>). -
Sharing files or directories across file systems.
-
Redirecting paths during system maintenance (e.g., old paths pointing to new locations).
5. Considerations
-
When using
<span>rm</span>to delete links, both hard links and symbolic links will be deleted, but hard links will not affect the data of other links. -
When using
<span>cp</span>to copy symbolic links, by default, the content of the target file is copied. Use<span>cp -d</span>to copy the link itself. -
When using
<span>mv</span>to move symbolic links, the link itself is moved without affecting the target file.
6. Conclusion
-
Hard Links: Multiple names for a file pointing to the same inode, safe but limited.
-
Symbolic Links: “Shortcuts” that are flexible but depend on the target’s existence.