21

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?

terdon
  • 98,183
  • 15
  • 197
  • 293
Justin
  • 455
  • 1
  • 3
  • 12
  • 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 '14 at 09:36
  • 3
    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 '14 at 10:37
  • Related: [Removing folder named ~](http://askubuntu.com/questions/26466/removing-folder-named), [Unable to “rm” a file named “() - .” due to “No such file or directory”](http://askubuntu.com/questions/225055/unable-to-rm-a-file-named-due-to-no-such-file-or-directory), [rm file '??q:q '](http://askubuntu.com/questions/389138/rm-file-qq) – Ilmari Karonen Jun 14 '14 at 09:58
  • For future reference: [This question](http://unix.stackexchange.com/q/147377/49168) has answers for all the different possibilities to remove files. There are quite a few... – jmiserez Aug 05 '14 at 14:47

4 Answers4

45

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
    
terdon
  • 98,183
  • 15
  • 197
  • 293
  • 1
    For the third option, `find . -name "-r" -delete` is even shorter. You may also want to use `-maxdepth 1` if you only want to delete the offending file in the current directory, but not recursively in all sub-directories. – Malte Skoruppa Sep 17 '15 at 22:46
  • @MalteSkoruppa true, I tend to avoid GNU-only options but there's no reason to on this site. – terdon Sep 18 '15 at 11:33
26

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.

Benoit
  • 7,497
  • 1
  • 24
  • 34
9

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
terdon
  • 98,183
  • 15
  • 197
  • 293
Sylvain Pineau
  • 61,564
  • 18
  • 149
  • 183
2

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

Example:

rm -rf ~/Desktop/-r
Avinash Raj
  • 77,204
  • 56
  • 214
  • 254