Score:0

how to delete a file by a inode in linux kernel

cn flag

I got the inode of a file in linux kernel, I want to delete the file by the inode.

Do I need to unlink every dentry of the inode? or do I need to unlink any dentry?

Which locks should I use to protect what?

Score:2
cn flag

Portable file remove calls, unlink(), remove(), and higher level wrappers, pretty much all take path names. Inodes are mostly an implementation detail.

To do this in shell, make use of tools like GNU find.

find /tmp  -type f -inum $INODE

Replace /tmp with the path to search, $INODE with the number. Add -delete if you want the results unlinked. Multiple names of the same inode may be found.

As this method traverses through all files, it is not efficient.

Score:0
sd flag

In Linux, you can't delete a file.

What you can do is remove all references to it, and then the filesystem will automatically remove the space and mark the inode as unused.

But note that files can be referenced by running processes. The file will still exist even though nothing in the filesystem points to it. Until the process finishes or closes the file, it will still exist.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.