## 14.14. Using unlink to Remove a File with a Strange Name

Some versions of Unix have a lot of trouble with eight-bit filenames -- that is, filenames that contain non-ASCII characters. The ls -q (Section 8.12) command shows the nonASCII characters as question marks (`?`), but usual tricks like rm -i * (Section 14.12) skip right over the file. You can see exactly what the filename is by using ls -b (Section 8.12):

>     % ls -q
>         ????
>     afile
>     bfile
>     % rm -i *
>     afile: ? n
>     bfile: ? n
>     % ls -b
>     \t\360\207\005\254
>     afile
>     bfile

On older Unixes, the -b option to ls might not be supported, in which case you can use od -c (Section 12.4) to dump the current directory, using its relative pathname . (dot) (Section 1.16), character by character. It's messier, and isn't supported on all Unix platforms, but it's worth a try:

>     % od -c .
>        ...
>     00.....   \t 360 207 005 254  \0  \0  \0  \0  ...

If you can move all the other files out of the directory, then you'll probably be able to remove the leftover file and directory with rm -rf (Section 14.16, Section 14.10). Moving files and removing the directory is a bad idea, though, if this is an important system directory like /bin. Otherwise, if you use the escaped name ls -b gave you, you might be able to remove it directly by using the system call unlink (2) in Perl. Use the same escape characters in Perl that ls -b displayed. (Or, if you needed to use od -c, find the filename in the od listing of the directory -- it will probably end with a series of NUL characters, like `\0 \0 \0`.)

>     perl -e 'unlink("\t\360\207\005\254");'

-- JP

Copyright © 2003 O'Reilly & Associates. All rights reserved.