[Chapter 23] 23.15 Using unlink to Remove a File with a Strange Name
Author: Jerry Peek, Tim O'Reilly & Mike Loukides
Date: 1998-08-04T21:41:11Z

#

| Chapter 23 Removing Files |

## 23.15 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 (51.3) characters. The ls -q (16.14) command shows the non-ASCII characters as question marks (`?`), but usual tricks like rm -i * (23.12) skip right over the file. You can at least see exactly what the filename is by using od -c (25.7) to dump the current directory, using its relative pathname `.` (dot) (1.21), character by character. (Note: some versions of UNIX have an ls -b (16.14) option that will do the same thing as od -c, but a lot more easily.)

>     % ls -q
>         ????
>     afile
>     bfile
>     % rm -i *
>     afile: ? n
>     bfile: ? n
>     % 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 (23.17, 23.10). Moving files and removing the directory is a bad idea, though, if this is an important system directory like /bin.

Otherwise, if you can find the filename in the od listing of the directory (it will probably end with a series of NUL characters, like `\0 \0 \0`...), you might be able to remove it directly by using the system call unlink(2) in Perl. Put a backslash (`\`) before each of the octal bytes shown in the od output:

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

If you don't have Perl, write a little C program (52.8):

>     % vi unlink.c
>        ...
>     % cat unlink.c
>     main()
>     {
>         unlink("\t\360\207\005\254");
>     }
>     % cc unlink.c
>     % ./a.out

Another ls will tell you whether your program worked (there probably won't be any error messages if it doesn't work).

- JP

23.14 Handling a Filename Starting with a Dash (-) |  | 23.16 Removing a Strange File by its I-number