Appearance
27. Hard Link vs Soft Link
Hard link: a new directory entry pointing to the same inode — same data, same inode number. Deleting the original doesn't affect it; the data is only freed when the link count hits zero and no process has it open. Cannot cross filesystems (inode numbers are only unique within one filesystem).
Soft link (symlink): a new file with its own inode, whose content is just the path to the original. Breaks if the original is deleted (dangling link). Can cross filesystems and can link directories.
| Hard Link Soft Link | ||
|---|---|---|
| Original deleted | Still works | Breaks |
| Own inode? | No (shares inode) | Yes |
| Cross filesystem? | No | Yes |
| Can link a directory? | No | Yes |
| Size | Same as original | Just a path (tiny) |
Mental model: a hard link is like a second pointer to the same memory address (int* p2 = p1;); a soft link is a variable holding a path/address as a string. If the target is gone, the hard link still works (you hold the memory directly); the soft link dangles (the path leads nowhere).