Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

This question already has an answer here:

I have been creating odd-named files every once in a while, e.g.

$ls -rthl
$-rw-r--r--  1 shamil hep  290 Aug 13 11:58 -rf

And interestingly it is impossible to remove this file with

rm -f -rf

I know I need to escape the special symbols like "-", so the issue is clear. And I used to know the solution, but have since forgotten.

How do I properly delete it?

I have tried things like this

rm -f \-rf

but to no avail.

share|improve this question

migrated from stackoverflow.com Dec 17 '13 at 22:02

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

marked as duplicate by Gilles, Andrew Lambert, Kevin Panko, terdon, Tog Dec 19 '13 at 8:04

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.

    
Ty. Somehow I wasn't able to find it. –  Shamil Assylbekov Dec 17 '13 at 20:25
2  
Probably best to just haul out the big guns python -c 'import os; os.remove("-rf")' –  u2EF1 Dec 17 '13 at 20:26
    
@U2EF1 nice. I used to use that hack before learning the -- :) Sadly, not every distribution has Python, so it is also not very portable. –  Paulo Bu Dec 17 '13 at 20:28

3 Answers 3

up vote 12 down vote accepted

-- switch means: End of flags, everything after it is assumed to be a file name. So you can do:

rm -- -rf

Include the full path to the file also works:

rm /full/path/to/-rf

Actually, this is kind of hilarious but this command line can give you the answer :)

$man rm | cat | grep -B4 -A3 -- "rm --"

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
1  
This won't work with all commands. –  Donovan Dec 17 '13 at 20:25
    
Please can you provide an example? I was quoting the man bash there. –  Paulo Bu Dec 17 '13 at 20:26
    
@PauloBu man rm in bash? –  Mad Physicist Dec 17 '13 at 20:28
1  
@MadPhysicist not, actually I quote man bash. But seeing man rm there is a line that tells you that: man rm |cat |grep -B4 -A3 -- "rm --" –  Paulo Bu Dec 17 '13 at 20:33
2  
Whether this convention works with all commands is irrelevant, and a different answer to a different question. The questioner specifically asked about rm and deleting files and a way to invoke rm that xe had forgotten. Paulo Bu's answer deals specifically with that, even quoting the manual for rm where the questioner would have found xyr answer. Its major problem is a Useless Use Of cat. (-: –  JdeBP Dec 18 '13 at 13:38

You can do

rm -rf ./-rf

This will make it so that -rf does not start with a -.

share|improve this answer

another way:

find -name "-rf" -exec rm {} \;
share|improve this answer
    
This relies on undocumented behavior of find -- do you have any information regarding how many variants of *nix support it? Anyway, it’s just a roundabout way of saying rm ./-rf. –  Scott Dec 18 '13 at 0:28
    
@Scott find without a directory argument is specific to Linux (both GNU and BusyBox) and is equivalent to find .. The names listed by find . obviously don't start with - since they start with ./. –  Gilles Dec 18 '13 at 9:55
    
But that’s my point – how do you know that find . … -exec … builds command arguments beginning with ./? Sure, you might guess that it does, from the behavior of find . … -print (in contrast to, for example, ls . or ls -l .), and maybe it is standard, but it’s not documented. –  Scott Dec 18 '13 at 17:14

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