Deleting files with bad names in Linux

In an earlier post I talked about the usefulness of the Linux "find" command to delete old files. Another, perhaps less common, use is to delete files with "bad characters" in the name. For instance, the famous "tar" archiver allows you to exclude certain files by specifying the "--exclude=filename" option.

This works nicely unless you happen to misspell the word "exclude", which I did the other day. So, instead of having a fresh archive which consisted of exactly the files I wanted, I wound up with an archive which also included the files I didn't want and with a name I didn't expect.

Furthermore, because the double-dash now preceded the file name, and because a double-dash has special meaning to almost all shell commands, including "rm", I now had an archive which couldn't even be deleted in a conventional way. However, by using the "find" command and specifying a file "inode" number, I'm able to delete my badly named archive, irrespective of what characters are in the name:

  1. Use "ls -il" to get the file "inode" number.
  2. Use "find . -inum xxxxxx -exec rm -f {} \;" to delete the file.

Substitute "xxxxxx" in step 2 with your actual file "inode" number from step 1. This method works on files with other special or unprintable characters too. Just make sure you get the "inode" number correct.

Due to the large volume of spam, comments are disabled. If you have anything relevant to say, you can leave a , or contact me directly.

About the author

Ivan's mugshotI'm Ivan Lutrov and I'm the owner of Lutrov Interactive. I have 25 years of experience producing interactive work and I create cost effective business websites that are simple, engaging and easy to use. I preach what I practice, and I say what I really think, even if it's sometimes not what you expect to hear. Subscribe to the Lutrov Interactive feed via RSS and follow me on Twitter.