How to delete a file called “-rf”?

This seems to flummox a lot of people. The worst candidates won’t even notice there’s a problem with this. The best tend be able to come up with at least two possible solutions, possibly more.

 Bad answers

rm -rf

A surprising number of people will say this

rm *

You’d better hope “-rf” isn’t the first file on that list

rm -i *

“-f” overrides “-i” and you’ve just deleted everything in the directory.

rm "-rf"

This handily escapes “-rf” to make sure rm will get these options and the file will stay there. Shows a lack of understanding of what escaping shell arguments does.

 Good answers

rm -- -rf

If they can explain that “–” separates arguments from files then that’s good. This is useful for other situations.

ls -i; find . -inum <inode for file> -delete

Handy way to show some knowledge of what an inode is.

rm ./-rf

Nice and simple.

perl -e 'unlink("-rf");'

The unlink function in perl doesn’t see anything special about “-rf” and will happily delete the file.

find ./ -name -rf -exec rm {} \;

“find ./ -name -rf” outputs “./-rf” so this is effectively the same as “rm ./-rf”. It does work however.