Sobes.tech
Junior — Middle

How does the Linux operating system handle the deletion of a hard link to a file?

sobes.tech AI

Answer from AI

In Linux, deleting a hard link to a file means removing one of the references to the file's inode. In Linux, a file is represented by an inode, which contains metadata and points to data on the disk. Hard links are names that point to the same inode.

When a hard link is deleted (for example, using rm), the operating system decreases the link count of the inode. If, after deletion, the link count becomes zero and there are no open file descriptors for the file, the kernel frees the space occupied by the file on the disk.

Thus, the file is physically deleted only when there are no hard links and no processes holding the file open.