0

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?

2 Answers2

2

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.

John Mahowald
  • 32,050
  • 2
  • 19
  • 34
0

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.

Ray Butterworth
  • 146
  • 1
  • 1
  • 8