Introduction to Linux Links
In Linux file systems, links provide a mechanism to access files from multiple locations. There are two types of links: Hard Links and Soft Links (also known as Symbolic Links). By default, the ln command creates a hard link. To create a symbolic link, the -s flag must be specified.
Hard Links
A hard link is a direct reference to the data on the physical disk via an Inode number. In Linux, every file is assigned a unique Inode Index, wich serves as a pointer to the actual data blocks.
Multiple filenames can point to the same Inode. This effectively creates different entry points for the same data. A significant advantage of hard links is data safety. Deleting a filename (a hard link) only removes that specific reference. The actual data on the disk remains intact and accessible through other links pointing to the same Inode. The file system only deallocates the data blocks when the last hard link to the file is deleted.
This principle is widely used in storage snapshot technologies (like those in NetApp storage) where snapshots are essentially hard links, ensuring that data is not lost even if the original file path is deleted.
Symbolic Links (Soft Links)
A symbolic link, or soft link, functions similarly to a shortcut in Windows. It is a special type of file that contains a text string interpreted by the operating system as a path to another file or directory.
Unlike hard links, symbolic links have their own unique Inode number. They merely point to the file path, not the underlying data. If the original target file is moved or deleted, the symbolic link becomes "broken" and useless.
Practical Demonstration
The following example illustrates how to create both types of links and inspect their Inode numbers.
# Create a workspace and a source file
mkdir /data/project && cd /data/project
touch source_data.txt
# Create a Hard Link (default behavior)
ln source_data.txt hard_link_ref
# Create a Symbolic Link (requires -s flag)
ln -s source_data.txt soft_link_ref
# Inspect Inodes and file types
ls -li
Running ls -li will reveal that source_data.txt and hard_link_ref share the exact same Inode number, while soft_link_ref has a distinct Inode.
Key Differences Summary
- Inode Number: Hard links share the same Inode as the target file. Symbolic links have a different Inode.
- Directories: Hard links cannot be created for directories to prevent file system loops. Symbolic links can point to directories.
- Cross-Filesystem: Hard links cannot cross partition boundaries. Symbolic links can link across different file systems.
- Source Deletion: If the original file is deleted, the hard link retains the data. A symbolic link will fail to resolve.
Real-World Applications
Symbolic Links: These are often used to manage software versions. For instance, an Apache web server might be installed at /usr/local/apache-2.4.52. An administrator might create a symbolic link /usr/local/apache pointing to this versioned directory. When upgrading, the link is simply updated to point to the new version, simplifying configuration management.
Hard Links: These are used in storage snapshots and backup systems. By creating hard links for backup points, administrators can ensure that data is not accidentally purged, as the data blocks remain allocated until all links are removed.
Understanding Directory Link Counts
When creating a new directory using mkdir, you can verify its link count using ls -ld. You will notice the count starts at 2.
This occurs because:
- The directory entry itself counts as one link.
- The hidden
.entry inside the new directory is a hard link pointing back to the directory itself.
Consequently, a new directory always has a link count of 2.