Delete Files With Special Characters

I use SecureCRT for my terminal application, partly because it supports native zmodem transfers, and that makes moving files back and forth between my desktop super easy (if you have lrzsz installed you can just “sz filename.txt” to send something over). Occasionally, though, the transfer aborts and the shell vomits things to files with special characters in their names, like:

$ ls
-rw-r--r--.  1 plankers plankers     0 May  9 14:00 ''$'\326''y'$'\342''['$'\305''X'
-rw-r--r--.  1 plankers plankers     0 May  9 14:00 ''$'\370\343''4'$'\361'

How do you deal with files with special characters? There are a number of tricks that work:

  • Move everything else out of the directory and “rm -rf” that directory.
  • Use a GUI of some sort to remove them.
  • Try to escape the special characters with ‘\’ so the shell doesn’t interpret them.
  • Use Samba to view the directory from a Windows PC and delete them.
  • Use the “find” command with the inode number.

The inode number method is super easy to use if you have shell access, so let’s try that. You can see the inode number by using “ls -i” or “ls -li

$ ls -li
6583 -rw-r--r--. 1 plankers plankers 0 May 9 14:00 ''$'\326''y'$'\342''['$'\305''X'
6584 -rw-r--r--. 1 plankers plankers 0 May 9 14:00 ''$'\370\343''4'$'\361'

In this case the inode numbers are 6583 and 6584. From here, you can use the “find” command:

$ find . -inum 6583
./?y?[?X

And when you confirm that’s what you want to go away, append the “-delete” flag:

$ find . -inum 6583 -delete

And that is how you delete files with special characters!

Posted in “Tips & Tricks” and “Linux” — you might be interested in other posts in those categories.