Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

This question already has an answer here:

I inadvertently created a file called -r in my home directory. Now I cannot get rid of it:

rm -rf
rm: missing operand
Try 'rm --help' for more information.

Other attempts:

rm /-/r
rm: cannot remove ‘/-/r’: No such file or directory

Another one:

rm \-r
rm: missing operand
Try 'rm --help' for more information.

Is there a way to remove this file without deleting the whole directory?

share|improve this question

marked as duplicate by Gilles, Radu Rădeanu Aug 5 at 17:48

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1  
Just FYI: Your second try using the forward slashes would delete a file named r from a directory named - in the root directory. –  Dubu Jun 12 at 9:36
2  
From man rm: To remove a file whose name starts with a -, 'for example -foo', use one of these commands: rm -- -foo or rm ./-foo –  Parto Jun 12 at 10:37
    
    
For future reference: This question has answers for all the different possibilities to remove files. There are quite a few... –  jmiserez Aug 5 at 14:47

5 Answers 5

up vote 24 down vote accepted

In this case you have to use the double-dash (--) in your command arguments.

The purpose of it is to tell to the command that what's follow has not to be taken as an argument to the command but a simple input. In the case of rm, a file or directory name.

So type rm -- -r and you should get rid of this file.

share|improve this answer

There are many ways of doing this:

  1. Use the -- which signifies the end of option flags and the beginning of the list of arguments for many programs (including rm).

    rm -- -r
    
  2. Use the full path

    rm /home/you/directory/-r
    

    or, from the same directory (your current directory is referred to as .):

    rm ./-r
    
  3. Use find

    find . -name "-r" -exec rm {} \;
    

    or, to get all such files

    find . -name "-*" -exec rm {} \;
    
  4. Use an ugly hack. Move everything to a different directory (this will fail for the -r file for the same reason as rm does) and then delete the original directory (which will remove the file) and move everything back again. So, assuming your -r file is in ~/foo:

    $ mkdir ~/bar
    $ for f in *; do mv "$f" ../bar/; done
    mv: invalid option -- 'r'
    Try 'mv --help' for more information.
    $ rm -rf ~/foo
    $ mkdir ~/foo && cd ~/foo
    $ mv ~/bar/* .
    $ rmdir ~/bar
    
share|improve this answer

You can also delete such files (starting with a '-') with this command:

rm ./-r

See the rm man page:

To remove a file whose name starts with a '-', for example '-foo', use one of these commands:

rm -- -foo

rm ./-foo
share|improve this answer

Or you could specify the full path of the directory in the rm command,

Example:

rm -rf ~/Desktop/-r
share|improve this answer

You may also use the unlink command (if you have it), which takes no options, and a single argument. So, unlink -- -r will remove a file called -r

share|improve this answer
    
Only unlink -- -r worked for me –  Sylvain Pineau Jun 12 at 13:59
1  
I think the most recent edit, intended to improve the answer in light of Sylvain Pineau's post, actually makes it worse. Now the text doesn't agree with the example command... and even if the text were changed to make it agree, I think it still wouldn't be right. I suspect the truth is that some systems are as Abigail described (unlink takes no options so unlink -r will work and unlink -- -r won't) and other systems are as Sylvain Pineau reports (unlink -- -r works, unlink -r doesn't). So useful advice might be "you could also use unlink, although it is less portable than other methods..." –  Don Hatch Jun 14 at 3:05

Not the answer you're looking for? Browse other questions tagged or ask your own question.