Deleting files with bad names in Linux
Tuesday, 18 July 2006
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:
- Use
"ls -il" to get the file “inode” number.
- 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.
|
Great tip. Had a file named "-r.new" and it blew it away. Thanks!