Next: , Previous: Cleaning Up, Up: Common Tasks


9.4 Strange File Names

find can help you remove or rename a file with strange characters in its name. People are sometimes stymied by files whose names contain characters such as spaces, tabs, control characters, or characters with the high bit set. The simplest way to remove such files is:

     rm -i some*pattern*that*matches*the*problem*file

rm asks you whether to remove each file matching the given pattern. If you are using an old shell, this approach might not work if the file name contains a character with the high bit set; the shell may strip it off. A more reliable way is:

     find . -maxdepth 1 tests -okdir rm '{}' \;

where tests uniquely identify the file. The ‘-maxdepth 1’ option prevents find from wasting time searching for the file in any subdirectories; if there are no subdirectories, you may omit it. A good way to uniquely identify the problem file is to figure out its inode number; use

     ls -i

Suppose you have a file whose name contains control characters, and you have found that its inode number is 12345. This command prompts you for whether to remove it:

     find . -maxdepth 1 -inum 12345 -okdir rm -f '{}' \;

If you don't want to be asked, perhaps because the file name may contain a strange character sequence that will mess up your screen when printed, then use ‘-execdir’ instead of ‘-okdir’.

If you want to rename the file instead, you can use mv instead of rm:

     find . -maxdepth 1 -inum 12345 -okdir mv '{}' new-file-name \;