I am unable to delete a filename named -r
with rm -rf -r
Is there any special trick to remove it?
Asked
Active
Viewed 1,112 times
3

cstamas
- 6,607
- 24
- 42

Niketan Raval
- 149
- 4
-
5I'm voting to close this question as off-topic because the answer is readily available in the man pages. – Reaces Jul 14 '16 at 11:40
-
I spam tab until it selects the strangely named file – Martijn Jul 14 '16 at 16:55
2 Answers
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
-
2I'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
-
5Just 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