What is the difference between a hard link and a symbolic link in Linux?
Compare hard links and symbolic links in Linux: inodes, deletion behavior, filesystem limits, and when to use ln versus ln -s, with clear examples.
Expected Interview Answer
A hard link is an additional directory entry pointing to the same inode (the actual file data) as the original, while a symbolic link is a small separate file that stores a path pointing to another file by name.
Because a hard link shares the same inode, it is indistinguishable from the original and the file's data survives until the last hard link is removed. A symbolic link, by contrast, has its own inode and simply holds a path, so it breaks if the target is moved or deleted. Hard links cannot cross filesystem boundaries or link to directories, whereas symbolic links can span filesystems and point to directories. Deleting the original file leaves hard links fully working but turns symbolic links into dangling references.
- Hard links keep data alive until the last reference is gone
- Hard links add no meaningful storage overhead
- Symbolic links can span different filesystems
- Symbolic links can point to directories
- Each serves distinct backup and organization needs
AI Mentor Explanation
A hard link is like two official scorebooks that both record onto the very same set of pages: erase one book and the other still holds every run because the pages themselves survive until both are discarded. A symbolic link is a note taped to the pavilion saying 'scores are in Room 5' — if Room 5 is renamed or cleared, the note points nowhere, exactly like a broken symlink whose target has moved.
Step-by-Step Explanation
Step 1
Explain the inode
Describe how a file's actual data lives in an inode, and directory entries are names pointing to inodes.
Step 2
Define a hard link
State that a hard link is another directory entry pointing to the same inode, sharing identical data and link count.
Step 3
Define a symbolic link
State that a symlink is a separate file with its own inode that stores the path to another file.
Step 4
Compare deletion behavior
Show that removing the original leaves hard links working but leaves symlinks dangling.
Step 5
List the constraints
Note that hard links cannot cross filesystems or target directories, while symlinks can do both.
Step 6
Demonstrate creation
Use ln for hard links and ln -s for symbolic links, then inspect with ls -li and stat.
What Interviewer Expects
- Understanding of inodes and directory entries
- That hard links share the same inode and link count
- That symlinks store a path and have their own inode
- Awareness of filesystem and directory constraints on hard links
- Knowledge of ln versus ln -s and dangling links
Common Mistakes
- Saying a hard link is just a copy of the file
- Claiming deleting the original breaks a hard link
- Thinking symbolic links share the target's inode
- Forgetting hard links cannot span filesystems
- Believing symlinks cannot point to directories
Best Answer (HR Friendly)
“A hard link is a second name for the very same file data, so the file stays until every name is removed. A symbolic link is more like a shortcut that just points to a file by its location, and it stops working if that file is moved or deleted.”
Code Example
# Create a hard link (shares the same inode as original.txt)
ln original.txt hardlink.txt
# Create a symbolic link (stores the path to original.txt)
ln -s original.txt symlink.txt
# Compare: matching inode numbers reveal the hard link
ls -li original.txt hardlink.txt symlink.txt
# Deleting the original leaves the hard link intact
rm original.txt
cat hardlink.txt # still works
cat symlink.txt # dangling: No such file or directoryFollow-up Questions
- Why can't a hard link cross filesystem boundaries?
- What happens to a symbolic link when its target is deleted?
- How does the inode link count change as you add hard links?
- Why are hard links to directories generally disallowed?
- How do you find all hard links pointing to the same inode?
MCQ Practice
1. What do a file and its hard link share?
A hard link is another directory entry pointing to the same inode, so both names reference identical file data.
2. What happens to a symbolic link when its target file is deleted?
A symlink only stores a path, so deleting the target leaves it dangling and pointing to a nonexistent file.
3. Which statement about hard links is true?
Hard links reference an inode within one filesystem, so they cannot span filesystems, and linking to directories is generally disallowed.
Flash Cards
What does a hard link point to? — The same inode as the original file, making it an equal name for the identical data.
What does a symbolic link store? — A path to another file, in its own separate inode, resolved by name at access time.
What happens when you delete the original of a hard link? — Nothing breaks; the data survives until the last hard link is removed and the link count reaches zero.
What are the hard link constraints? — It cannot cross filesystem boundaries and generally cannot link to a directory.
How do you create each link? — Use ln target name for a hard link and ln -s target name for a symbolic link.