Operating Systems Concepts & Design
Since we’ve already discussed how inodes and data blocks work, understanding the difference between hard and symbolic (soft) links is much easier. Both are methods for making a single file appear in multiple locations, but they operate at different layers of the file system.
A hard link is essentially an additional name for an existing inode. When you create a hard link, you aren’t pointing to the file; you are pointing directly to the inode number where the data lives.
/dev/sda1 to a folder on /dev/sdb1 because inodes are unique only within their own partition).A symbolic link is a special type of file that contains a text string: the path to another file or directory. It is the Linux equivalent of a “Shortcut” in Windows.
Imagine a library (the file system):
The ln (link) command handles both.
| Task | Command | Identification |
|---|---|---|
| Create Hard Link | ln target linkname |
ls -i (Look for identical Inode numbers) |
| Create Symlink | ln -s target linkname |
ls -l (Look for l at the start of permissions and an arrow ->) |
| Feature | Hard Link | Symbolic Link (Soft) |
|---|---|---|
| Inode | Shared with the target | Unique to the link |
| Cross-Filesystem | No | Yes |
| Link to Directory | No | Yes |
| If Target is Moved | Still works | Breaks |
| Space Used | Zero extra (just a name entry) | Minimal (stores path string) |
In your work with containers or web servers, you’ll often see symlinks used for versioning. For example, a folder named current might be a symlink to v1.2.0. When you’re ready to upgrade, you just update the symlink to point to v1.3.0. This allows for zero-downtime updates!
Directories use links in a very specific, structural way to maintain the “tree” hierarchy of the Linux filesystem. While you generally cannot create your own hard links to directories as a user, the system creates them automatically to handle navigation.
Every time you create a directory, the filesystem automatically creates two hidden hard links inside it. These are essential for the OS to understand where it is and how to go back.
. (Dot): This is a hard link to the directory itself. It points to the same inode as the directory’s name in its parent folder... (Dot-Dot): This is a hard link to the parent directory.Why this matters for Inode Counts:
If you look at the “Links” column in ls -l, a brand-new, empty directory will show a link count of 2.
. inside the folder itself.If you create a sub-directory inside it, the parent’s link count jumps to 3 because the new sub-directory’s .. now points back to it.
Linux (and most Unix-like systems) strictly forbids users from creating their own hard links to directories.
/home/user/dir_a into /home/user/dir_a/subdir, you would create a circular directory structure.find, du, or backup scripts would get stuck in an infinite recursion, walking in circles until the system crashes or runs out of memory.. and .. links because the kernel can guarantee they follow a strict hierarchical “tree” logic.Because hard links are restricted, Symbolic Links (Symlinks) are the standard way to “shortcut” into directories.
web_root to a specific versioned folder like project_v2.1.sites-available into sites-enabled.You can actually calculate how many sub-directories a folder has just by looking at its hard link count:
ls -ld /var.. link).| Link Type | Used for Directories? | Purpose |
|---|---|---|
| Hard Link | System Only | Created as . and .. to build the tree. |
| Symbolic Link | User & System | Used for shortcuts, versioning, and cross-disk access. |