How to remove a file with name starting with "-r" using cli

5

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.

Chikipowpow

Posted 2013-12-17T20:20:47.770

Reputation: 163

Question was closed 2013-12-19T08:04:12.987

Even more discussion at How do I delete a file whose name begins with “-” (hyphen a.k.a. dash or minus)? (on U&L).

– Scott – 2015-03-26T04:00:26.307

Ty. Somehow I wasn't able to find it. – Chikipowpow – 2013-12-17T20:25:53.947

2Probably best to just haul out the big guns python -c 'import os; os.remove("-rf")' – u2EF1 – 2013-12-17T20:26:11.837

@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 – 2013-12-17T20:28:19.673

Answers

13

-- 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

Paulo Bu

Posted 2013-12-17T20:20:47.770

Reputation: 268

1This won't work with all commands. – Donovan – 2013-12-17T20:25:24.650

Please can you provide an example? I was quoting the man bash there. – Paulo Bu – 2013-12-17T20:26:55.337

@PauloBu man rm in bash? – None – 2013-12-17T20:28:46.747

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 – 2013-12-17T20:33:20.903

The double dash trick is handled by the command line processing library. It is not guaranteed to work all of the time. Try this script `#!/bin/sh

for i in $@ do echo $i done` – Sean Perry – 2013-12-17T23:00:42.590

@Donovan -- works with all commands that respect the option parsing conventions, which is almost all of them. – Gilles 'SO- stop being evil' – 2013-12-18T09:51:17.347

@SeanPerry This script doesn't accept any options. (It's also missing some essential quoting, to do something halfway sensible.) Almost all commands do accept options and do obey the conventions.

– Gilles 'SO- stop being evil' – 2013-12-18T09:54:22.907

2Whether 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 – 2013-12-18T13:38:04.300

I also offered another alternative which might work whether the shell support -- or not. That is the full path. Anyways, I hope someone find my answer useful :) – Paulo Bu – 2013-12-18T13:46:52.567

8

You can do

rm -rf ./-rf

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

Mad Physicist

Posted 2013-12-17T20:20:47.770

Reputation: 3 270

2

another way:

find -name "-rf" -exec rm {} \;

BMW

Posted 2013-12-17T20:20:47.770

Reputation: 137

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 – 2013-12-18T00:28:40.147

@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 'SO- stop being evil' – 2013-12-18T09:55:42.677

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 – 2013-12-18T17:14:19.163