1

I am wondering how the internal mechanism of file deletion works on Unix.

If there are some hard links pointing to an actual file, do I need to delete all links in order to delete the file?

If I delete the file, will the hard links be destroyed or deleted automatically?

On the other hand, if I use symbolic links to point to a file what should be done to delete the file?

Finally, the space of the file will be free for another use when all links pointing to it are destroyed. Is that correct?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
mgus
  • 808
  • 4
  • 17
  • 39

1 Answers1

2

A "file" has some content and one or more hard links to this data, ie. one or more "path+filename". If there is a file with multiple file names, in order to delete the content, you need to delete all hard links,
ie. all file names of it.
You can´t delete the file as in file content directly, you can only delete hard links.
If all links to the content are destroyed, the content itself will be deleted (at least marked as free space)

Symlinks: Deleting a symlink does not delete the file content
nor the actual hard link which the symlink is pointing to

deviantfan
  • 11,268
  • 3
  • 32
  • 49
  • OK. So, every file has necessarily at least one hard link (which we need to delete in order to delete it) and optionally some symlinks. Is that right? – mgus Jan 03 '14 at 15:27
  • 1
    Correct. A newly created file = content and one hard link. Symlinks on the other side could be understood as the only "real" links, as they are not part of the target file itself, but somthing like "if you want to open me, please see /home/user/hi.txt" – deviantfan Jan 03 '14 at 15:35
  • Note that on Unix, there are two conditions that must be met before the space used by a file is released. (1) There must be no names for the file — so no entries in the file system associate the inode number with a name, and (2) no process can have the file open. Until the last process closes the file (e.g. because it terminates), the file's space will remain in use, not free. The file can continue to grow, too. The inode keeps a record of the number of names a file has (the link count seen in `ls -l` listings). – Jonathan Leffler Jan 03 '14 at 15:57