3

I am unable to delete a filename named -r with rm -rf -r Is there any special trick to remove it?

cstamas
  • 6,607
  • 24
  • 42

2 Answers2

16

In Linux or Unix-like system you may come across file names with special characters such as:

-
--
;
&
$
?
*
White spaces, backslashes and more.

The problem and solution

Tip #1: Try a ./ at the beginning of the filename

The syntax is as follows to delete a file called '-file':

$ rm -v ./-file

removed `./-file'

Tip #2: Try a -- at the beginning of the filename

A -- signals the end of options and disables further option processing     by   shell. 

Any arguments after the -- are treated as filenames and arguments.

$ rm -- -file

$ rm -- --file

$ rm -- '@#$%^&file'

$ rmdir -- '--dirnameHere'

Monty Harder
  • 633
  • 4
  • 9
Sagar Vaghela
  • 276
  • 1
  • 4
  • 2
    I'd replace the double quotes with single quotes, to avoid having the shell try to interpret things like $ and *. It's a good habit to get into in general. I've submitted an edit to that effect. – Monty Harder Jul 14 '16 at 14:50
  • 5
    Just for clarity's sake: The `--` should of course not be in the beginning of the filename, but previously as a separate argument. The examples are right, of course, only the bolded description is confusing. – ilkkachu Jul 14 '16 at 15:44
12

As described in the rm man pages:

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

      rm -- -foo
      rm ./-foo

So, in your case:

rm -- -r

Or

rm ./-r
faken
  • 3,044
  • 1
  • 12
  • 7