Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm always wondering: most GNU/Unix tools take options in the form "minus something", sometimes followed by an argument. What if you got a file named minus something?

$ ls
$ rm -f
$ ls
$ mv -f abc
mv: missing destination file operand after `abc'
Try `mv --help' for more information.
$ cat -f
cat: invalid option -- 'f'
Try `cat --help' for more information.


$ ls
$ gcc -ohello -ohello.c
gcc: fatal error: no input files
compilation terminated.

This is just out of curiosity; I don't have a use case for this.

share|improve this question

migrated from May 3 '12 at 8:07

This question came from our site for professional and enthusiast programmers.

You have to find a way of passing literally the string "-f" to the system call. Normally that's by careful escaping. – Flexo May 1 '12 at 14:11
To the 'close because off-topic' voter: This is a question about shell programming and how to avoid a problem. It is totally on-topic for SO. (OTOH, it is probably a duplicate; the problem is finding the relevant other question.) – Jonathan Leffler May 1 '12 at 14:15
possible duplicate of How do I delete a file named "-p" from bash? – grawity May 3 '12 at 8:09
up vote 11 down vote accepted

To remove a file named -x, use rm -- -x (-- means end of options) or rm ./-x.

share|improve this answer

It is fairly common to ask this type of question in interview settings. A common way to handle files with dashes is either:

$ rm -- -f
$ rm ./-f
share|improve this answer

A common question in Unix. The main way is to give the full path name to the file, so it doesn't have a dash in front of it:

$ rm -file.txt
unknown option -l

$ rm ./-file.txt    #No problem!
$ rm $PWD/-file.txt #Same thing

Some commands, you can use a dash by itself (or a double dash) to end the options. However, this is not necessarily true with all commands, or even the same command on different systems.

$ rm -- -file.txt   #Works on Linux but not on some Unix systems
share|improve this answer

you have to use

  rm -- <filename>


  rm -- -f
share|improve this answer
Some remark wouldn't be wrong. – user unknown May 2 '12 at 0:40

You must log in to answer this question.