I understand the different between hardlinks and softlinks in Linux, but I am having trouble understanding this one problem: Can a hard link ever point to a deleted file? Why or why not? I think it can but I am not certain. An explanation would be great. Thank you!
-
6Hint: a file is considered deleted when its hardlink count is 0 – Trash Can Jan 31 '18 at 02:32
-
1The explanation is simpler and more fundamental than inodes. Instead of asking about deleted files, realize the truth: There is no "delete" operation. The formal name of the operation you think of as "delete" is actually [`unlink`](https://linux.die.net/man/2/unlink) – Ben Voigt Jan 31 '18 at 05:09
3 Answers
Consider an example,
$ touch aFile.txt
$ ls -i aFile.txt # -i is the option to look at the inode of a file
2621520 aFile.txt
$ ln aFile.txt 2File.txt # Hardlink the file to another one
$ ls -i 2File.txt
2621520 2File.txt # inode is pointing to the same location
$ rm aFile.txt # Original file gets deleted
$ ls 2File.txt
2File.txt
$ ls -i 2File.txt # inode survives and still pointing to the same location
2621520 2File.txt
Read here more on inodes
.
EDIT:
stat
can show you the number of hardlinks of a file. You can use -c '%h'
option to view that:
# after the hardlink to 2File.txt
$ stat -c '%h' aFile.txt
2

- 11,119
- 5
- 34
- 52
-
1Very nice answer +1, just a little remark you can add `stat aFile.txt` and `stat -c 2File.txt` to display more file storage related information (more espacially `Links: 2`) or use `stat -c %h aFile.txt` to count the number of hardlinks to it. and the answer would be more than perfect! – Allan Jan 31 '18 at 02:46
-
1This is a very nice example! There were other past examples on stack overflow that didn't make sense to a beginner like me but this made sense. Thank you! – user8930130 Jan 31 '18 at 02:48
-
The comment "Original file gets deleted" is misleading. Removing a link to a file does *not* delete the file. The comment "inode is pointing to the same location" is also misleading. Both aFile.txt and 2File.txt are links to the same file. That is, they both refer ("point") to the same inode. – William Pursell Jan 31 '18 at 05:06
-
@WilliamPursell I somewhat disagree about this. Here for demo purpose, "deleted" shows that a user tried to remove the file, but finds the hard link intact. This tells two things, how "this" is different from a symlink and what (intuitively) that "delete" means, thus implying the importance/functionality of `inode`. – iamauser Jan 31 '18 at 14:54
Hard links are point to the same inode in the file system. I see like it a mirror, if you write into one of the hard links, the other will show the same information, at the end of the day you are writing in the same inode. Soft links are like in Windows the Shorcuts, if the original file is deleted the soft link get lost, and it is unusable. You already have a master answer with examples included, I hope you get it know. Kind Regards
A hard link will never point to a deleted file. A hard link is like a pointer to the actual file data. And the pointer is called "inode" in file system terminology. So, in other words, creating a hard link is creating another inode or a pointer to a file.
Having a pointer pointing to nothing is, useless at least, confusing. I mean, when you ls
, you see files there. But if you're told "There is no such file" when you open it, your reaction would be "$#%*!@?"
Having data without a pointer is useless, too. Because there is no way you can open the file. You lost your handle to it but it's there. This happens when your HDD/SSD crashed and your file system is corrupted. A recovery tool might find file data with reference count zero, and place them, in the cases of EXT2, 3, 4, in the lost+found
directory.
So, either cases, the Linux kernel doesn't allow you to create such links / inodes / pointers.

- 4,028
- 1
- 26
- 47