Thursday, February 18, 2010

removing a file when the filename starts with a dash

When you royally goof up a command, you may end up with some strange filenames.
$ echo "hi" > -oops.txt
And filenames that start with a dash can be tricky to delete.
$ rm -oops.txt
$ rm \-oops.txt
$ rm "-oops.txt"
All the above commands treat the -o as an option, and return the following error: rm: invalid option -- 'o'. Luckily, Ubuntu returns some extra help in the error message.
rm: invalid option -- 'o'
Try `rm ./-oops.txt' to remove the file `-oops.txt'.
Try `rm --help' for more information.
So all you need is a path on the front of that nasty filename.
$ rm ./-oops.txt

2 comments:

  1. You can also use the double-dash to indicate that no more options should follow:

    rm -- -oops.txt

    ReplyDelete