What is the difference between a hard link and a soft link in Linux?
A hard link is another name for the same file on disk (the same inode), so the data stays as long as any name exists. A soft (symbolic) link is a small separate file that stores a path to the target; it breaks if the target is moved or deleted, but it can point to directories and across file systems.
Explain it simply
Think of a person who is called "Rahul" at home and "R. Sharma" in the college register. Both names refer to the same person. If the register entry is removed, Rahul still exists. That is a hard link: two names, one actual file.
Now think of a note on the notice board saying "For doubts, go to Room 204". The note is not the teacher; it only tells you where to look. If the teacher moves to another room, the note now sends you to an empty room. That is a soft link (symlink): a pointer to a path, which can go stale.
Example
echo "hello" > original.txt
ln original.txt hard.txt # hard link
ln -s original.txt soft.txt # soft (symbolic) link
ls -li1311 -rw-r--r-- 2 ravi ravi 6 Oct 2 11:00 hard.txt
1311 -rw-r--r-- 2 ravi ravi 6 Oct 2 11:00 original.txt
1318 lrwxrwxrwx 1 ravi ravi 12 Oct 2 11:00 soft.txt -> original.txtNotice three things. hard.txt and original.txt share inode number 1311 and both show a link count of 2. soft.txt has its own inode, starts with l, and its size (12) is just the length of the text "original.txt".
Now delete the original:
rm original.txt
cat hard.txt # still prints: hello
cat soft.txt # error: No such file or directoryHow it works
In Linux, a file's data and details (size, owner, permissions, where the blocks are) live in an inode. A directory is just a table mapping names to inode numbers. A hard link is one more row in that table pointing to the same inode, and the inode keeps a link count. The data is freed only when the link count reaches zero and no process has the file open.
A symbolic link is its own small file whose content is a path. When you open it, the kernel reads that path and follows it. Because it stores a path and not an inode, it can cross file systems and point to directories, but it also has no idea if the target disappears.
| Hard link | Soft link | |
|---|---|---|
| Same inode as target | Yes | No |
| Works after target is deleted | Yes | No (dangling) |
| Can link to directories | No (normally) | Yes |
| Can cross file systems or partitions | No | Yes |
| Command | ln target name | ln -s target name |
Real-world use
- Symlinks switch versions without changing config:
/usr/bin/python3 -> python3.12, or acurrent -> releases/2026-10-02link for zero-downtime deploys. - Nginx on Debian/Ubuntu enables sites by symlinking files from
sites-availableintosites-enabled. - Backup tools like
rsync --link-destuse hard links so unchanged files take no extra space across daily snapshots.
Common mistakes
- Creating a symlink with a relative path from the wrong folder.
ln -s target namestores the text you give it, and it is resolved relative to the link's own location, not your current directory. - Thinking deleting a hard link frees space. It only removes one name; the space is freed when the last name goes.
- Thinking a symlink has its own permissions. The
rwxrwxrwxshown is ignored; the target's permissions apply.
What to say in the interview
"A hard link is an additional directory entry pointing to the same inode, so both names are equally the file and the data survives until the link count drops to zero. Hard links cannot span file systems or point to directories. A soft link is a separate file containing a path to the target. It can cross file systems and point to directories, but if the target is deleted or moved the link becomes dangling. I create them with ln and ln -s, and ls -li shows the inode numbers to tell them apart."
Follow-up questions
- What is an inode, and what does it not store? (Hint: the file name.)
- Why are hard links to directories not allowed?
- How do you find all broken symlinks in a folder?
- What happens if you delete a file that a running process still has open?