Loading...
Linux Interview Questions

Soft Link and Hard Link

1. What is a hard link in Linux?
A) A hard link is a directory entry that directly points to the inode of a file. Multiple hard links can exist for the same file, and they all share the same data.

2. What is a soft link (symbolic link) in Linux?
A) A soft link (symlink) is a pointer that references the path of another file. It acts like a shortcut to the target file.

3. What is the difference between hard link and soft link?
A)

  • Hard Link: Points to the same inode, shares data.
  • Soft Link: Points to a file path, not the inode.

4. How do you create a hard link?
A)

ln source_file hardlink_name

5. How do you create a soft link?
A)

ln -s source_file softlink_name

6. What happens if the original file is deleted in case of a hard link?
A) The hard link still works because it points to the same inode. The data remains accessible until all hard links are removed.

7. What happens if the original file is deleted in case of a soft link?
A) The soft link becomes broken (dangling link), since it only points to the file path.

8. Can hard links span across different filesystems?
A) No, hard links cannot span different filesystems. Soft links can.

9. Can hard links be created for directories?
A) Normally no, except for . and .. created by the system. Soft links can point to directories.

10. How do you identify a hard link vs a soft link?
A)

  • Use ls -li → Same inode number = hard link.
  • Use ls -l → Soft link shows name -> target.

11. What is an inode and how does it relate to hard links?
A)  An inode stores metadata about a file (permissions, ownership, data blocks). Hard links share the same inode number.

12. How do you check the number of hard links a file has?
A)
Run:

ls -l

The second column shows the link count.

13. Which consumes extra disk space: hard link or soft link?
A)

  • Hard link: No extra space (just another directory entry).
  • Soft link: Small extra space (stores the target path).

14. What are broken (dangling) symlinks?
A) When the target file of a soft link is deleted/moved, the symlink still exists but points to a non-existent path.

15. How do you find and remove broken symlinks?
A)

find /path -xtype l

(to find)

find /path -xtype l -delete

(to remove)

16. What is the maximum number of hard links possible for a file?
A) It depends on the filesystem, but usually 65,000+ in ext4.

17. Can you replace a soft link target without deleting the link?
A) Yes, use ln -sfn new_target existing_symlink.

18. When would you prefer a soft link over a hard link?
A) When linking across filesystems.
When linking to directories.
When you want a clear “shortcut” that shows the target path.

19. When would you prefer a hard link over a soft link?
A) When you need backup references to a file.
When you want the file to remain available even if the original name is deleted.

20. How do you differentiate soft and hard link visually in ls -l output?
A)

  • Hard link: Looks like a normal file, same inode as original.
  • Soft link: Has an arrow (→) showing the target.
Leave a Reply

Your email address will not be published. Required fields are marked *