Topics: AIX, System Admin

Removing files by inode

You will encounter them from time to time: files with weird filenames, such as spaces, escape codes, or uncommon characters. These often can be very difficult to remove.

For example, files with a space at the end:

# touch "a file "
# ls
a file
It's not such a problem, if you created the file yourself, and you KNOW there is space at the end. Otherwise, it can be quite difficult to remove it:
# rm "a file"
rm: a file: A file or directory in the path name does not exist.
It can be even more ugly if there is a ^M in the file name:
# touch 'a^Mfile'
# ls a*
a
file
# ls file
ls: 0653-341 The file file does not exist.
And it will quickly become horrible if there are unprintable characters in file names, or a combination of all of the above. Or how about a file called "-rf /". Would do dare run the command: "rm -rf /" on your system, not knowing if this will wipe out all files, or just remove the file with the filename "-rf /"?

So, if you have a file with an awkward filename, or simply don't know the file name of a file, because it contains unprintable characters, escape codes, slashes, spaces or tabs, how do you safely remove it?

Well, you can remove files by inode. First, discover the inode of a file:
# ls -alsi
12294  0 -rw-r--r--  1 root  system  0 May 07 15:38 a file
In the example above, the inode has number 12294. Then simply remove it using the find command, and the -exec option for the find command:
# find . -inum 12294 -ls
12294  0 -rw-r--r--  1 root  system  0 May  7 15:38 ./a file
# find . -inum 12294 -exec rm {} \;




If you found this useful, here's more on the same topic(s) in our blog:


UNIX Health Check delivers software to scan Linux and AIX systems for potential issues. Run our software on your system, and receive a report in just a few minutes. UNIX Health Check is an automated check list. It will report on perfomance, capacity, stability and security issues. It will alert on configurations that can be improved per best practices, or items that should be improved per audit guidelines. A report will be generated in the format you wish, and the report includes the issues discovered and information on how to solve the issues as well.

Interested in learning more?