Deleted a File in Unix by inode.

Sometimes a buggy program will create a filename that you can’t easily type. Or maybe you just got a bit of line noise at the wrong moment. Either way, you are stuck with a file that you can’t rm.

  • The easiest way to get rid of such a file is to just use rm -i to ask for deletion of a larger set of files; for instance, if the filename was Qdzt #%@A < garbage > , you’d just do rm -i Qdzt*.
  • Try to get a wildcard pattern that will affect as few files as possible and make sure you specify -i. For instance, you do not want to do something like rm ?*”. That will delete all files in the current directory without warning.
  • Also be careful of “rm .*”, since “..” matches that wildcard expansion, and “..” just points to the directory above you. Imagine wiping out the entire filesystem. You don’t have permissions to do that, of course, but you can still mess up your own files.
  • Also make sure there are no spaces; “rm Qdz *” will remove the file “Qdz” which probably does not exist, then remove all of the files (“*”). This is why “rm -i” is such a popular command; the -i makes rm prompt you for each file it has its sights on.

Sometimes a wildcard does not work. If this is the case, you are forced to use the Unix powertool find. First you must find out the inode number of the file in question. The “inode” corresponds to the physical location of that file on the filesystem; it’s block on the hard drive, if you will. To find that out, just use the ls command with the ‘i’ switch:

ls -i

The first number on the line (far left column) will be the inode number. Then issue the following command:

find . -inum -exec rm -i {} \;

This find command will recursively search the current directory for the file who’s inode number is . Once found, it executes the command rm -i {}. The {} part expands to the file that was found. The \; is necessary to end the exec command.

This can also be generalized to rename a file; just replace “rm -i” to “mv” in the find command and add the new name to the command. Find is a very powerful tool. For more information, peruse the man pages for find.

/