Thursday, December 11, 2008

Deleting files with bad names

If a file with a bad name gets accidentally created, such as a name that begins with a hyphen "-", it can't be deleted with a normal remove command (rm). Use the "--" option to tell rm that no more options follow, then it can delete the file.

To delete a file whose file name begins with "-":

rm -- -bad-file-name
rm ./-bad-file-name

To delete a file with non-printable characters in the name:

Use shell wildcards, "?" for one character and "*" for zero or more characters. For example, if the file name "bad file name" can't be deleted, one of the spaces may in fact contain a hexademical value. Try:

rm bad?file?name

caution: use ls bad?file?name first to make sure you are not matching more files than you think with wildcards before deleting them.