Hard Links Vs Soft Links
1. Hard Links
📌 Definition
A hard link is another name (directory entry) that points to the same inode (same actual file).
🔑 Key Idea
👉 A file =
-
inode (actual file data + metadata)
-
name (directory entry)
👉 Hard link creates:
📖 Example
Now:
-
fileandfile2→ same file
Check inode:
Output:
✔ Same inode → same file
🔄 Behavior
-
Changing one file affects the other:
✔ Both show updated content
🔢 Reference Count
-
Each inode keeps a link count
-
Incremented when a hard link is created
-
Decremented when a link is removed
❗ Deletion Logic
👉 Only removes one link
✔ File still exists via file2
👉 Actual deletion happens when:
🚫 Limitations of Hard Links
-
❌ Cannot link directories (to avoid cycles)
-
❌ Cannot link across file systems
-
✔ Only works within same filesystem
2. Soft Links (Symbolic Links)
📌 Definition
A symbolic link is a special file that stores the path of another file
📖 Example
Now:
-
file2→ points to"file"(path)
🔑 Key Idea
👉 Soft link:
👉 Not the same inode
📊 Check Details
Output:
-
l→ symbolic link -
Stores path as data
🔍 Internal Structure
-
Soft link is a separate file
-
Contains:
-
Pathname of target file
-
Example:
⚠️ Dangling Links
❌ Error: file not found
👉 Because:
-
Soft link points to path
-
Path no longer exists
Key Differences
| Feature | Hard Link | Soft Link |
|---|---|---|
| Points to | Inode | File path |
| Inode number | Same | Different |
| File type | Regular file | Special file |
| Cross filesystem | ❌ No | ✔ Yes |
| Directory linking | ❌ No | ✔ Yes |
| Survives original deletion | ✔ Yes | ❌ No |
| Storage | No extra space | Stores path |
Conceptual Diagram
Hard Link
Soft Link
Why unlink() Makes Sense
👉 Now you understand:
-
unlink()removes:-
name → inode mapping
-
-
Not the file itself immediately
✔ File removed only when:
Summary
Emphasize to students:
-
Hard link = multiple names for same file
-
Soft link = shortcut to another file
👉 Core distinction:
-
Hard link → direct reference
-
Soft link → indirect reference (path-based)
Q: What happens if original file is deleted?
-
Hard link → still works
-
Soft link → broken (dangling)
Comments
Post a Comment