Does a closed file have an inode? What is the difference between an open and a closed file when it is deleted?
Asked at
AI answer
Yes. A regular file on a filesystem has an inode whether or not it is currently open. The inode stores metadata and references to data blocks; the filename in a directory points to the inode.
Opening a file creates a file descriptor that refers to a file object in the kernel. close() closes only that descriptor and does not, by itself, delete the file. rm removes the filename from the directory (decrementing the number of hard links). If the file is still open, its data and inode usually remain accessible through open descriptors until the last one is closed; after that, the space is freed. That is why a deleted but open file can still occupy space and appear, for example, in lsof +L1.
Also asked at RWB
- Why did you decide to change jobs / leave your current company?
- What is the key difference between TCP and UDP?
- What is the difference between git merge and git rebase? When should each approach be used?
- What is an inode in Linux?
- Which command is used to determine the route of a packet from your device to the target host?
Similar questions
- You found open, deleted files using lsof. What would you do with them next?
- What is an inode, what information does it contain, and what does it not contain? Where are inodes physically located?
- What are hard links and soft links (symlinks) in Linux, and what happens to a file if its symlink is deleted?
- A 20 GB log file was deleted with rm, but the disk space wasn’t freed. Why did that happen?
- What does strace show when a file is opened (open/openat)?