What command do I need to use to remove a file called `-rf`?

68

22

I did something crazy at some point that created a file called -rf on my filesystem.
Now I can't figure out how to delete it...

I've tried:

rm "-rf"
rm \-rf

These just exit immediately, arrgh!

Anyone know how to remove this file? Preferably without accidentally cleaning out my whole folder.

Pierre-Antoine LaFayette

Posted 2010-10-05T22:06:31.907

Reputation: 987

34Some nerve trying to rm a file called -rf from your system without remembering the exact syntax! – ChristopheD – 2010-10-05T22:08:58.220

3To explain why those don't work: Quotes and escaping are parsed by your shell (typically bash), and then the result of the parsing is passed to rm (in it's argc and argv) Thus, both of those result in the array ["rm", "-rf"] being passed to rm, which does what you would expect. – Thanatos – 2010-10-05T22:10:05.590

3

I think this is closer to a duplicate of this one from Unix and Linux stackexchange: http://unix.stackexchange.com/questions/1519/how-do-i-delete-a-file-whose-name-begins-with

– frabjous – 2010-10-05T22:30:06.843

12An old gag is to get a file named * into a folder. Include one named -rf as well and watch the fun ;-) – RBerteig – 2010-10-06T00:35:45.710

@RBerteig: mkdir './-rf ' (ls --classify, a common default in a shell alias, will append a /) – None – 2010-10-06T02:53:12.433

@Roger, I like that one. --classify didn't exist as an option for ls when I last used a Unix box on a regular basis (ca. late 80's). But now that it does, that is just perfect. I nearly got burned by a file named *.* in a folder on VMS (on a VAX 11/780, IIRC) when I was just starting out... and I know it was a common prank on PDP-10 and PDP-11 systems too. – RBerteig – 2010-10-06T08:34:43.537

Answers

128

unlink -rf

Or

rm -- -rf

Josh Lee

Posted 2010-10-05T22:06:31.907

Reputation: 1 553

48This is the most useful solution to learn IMO, because '--' to stop processing arguments is standardised across the GNU tools – Nathan O'Sullivan – 2010-10-06T07:43:13.780

1

To be more precise, anything using GNU getopt has --, which should mean most modern linux tools.

– Geoffrey Zheng – 2010-10-06T17:28:27.877

61

Another option:

rm ./-rf

... asuming your current directory is the one where the file resides.

martin clayton

Posted 2010-10-05T22:06:31.907

Reputation: 1 012

34

Alternatively you can always edit the directory its in and remove the file that way.

vim .

and then just delete the line with the file on it (using D, dd won't work).

oadams

Posted 2010-10-05T22:06:31.907

Reputation: 431

5nice trick! [Several useless chars] – Sergio Acosta – 2010-10-06T04:48:54.493

I didn't think this deserved the downvotes it had when I first saw this answer this afternoon, but I didn't have a chance to go verify that vim had a dired mode. Clearly, editing the actual directory itself in a text editor is neither recommended, nor possible without real expert hoop jumping. Note that emacs would work too, and for the same reason. – RBerteig – 2010-10-06T08:38:03.180

1Extra points for clever Vim use! :) – Pierre-Antoine LaFayette – 2010-10-06T10:55:40.523

nice to know ;) – schöppi – 2010-10-21T21:21:52.720

25

A generic technique for deleting weird filenames is to use.

 ls -li

to find the inode number of file and then use.

find ./ -inum <number of file> -delete

No need to remember all the special cases.

user51427

Posted 2010-10-05T22:06:31.907

Reputation: 349

4Didn't know find had the power to delete files. – zneak – 2010-10-06T02:05:28.860

This is also great for catching files with unknown names (starting with or including backspace characters or having extra whitespace at the end), provided you have access to a decent GNU find on your box. – Olfan – 2010-10-06T10:20:53.990

17

Even though I know about the "rm -- -filename" trick, generally when I somehow get a file with a leading - in its name that I want to remove I start a GUI file manager and do it from there, to eliminate the chance of mistakes.

Heptite

Posted 2010-10-05T22:06:31.907

Reputation: 16 267

1He may not have a GUI at all, so this might not work. – Wuffers – 2010-10-05T22:25:06.900

12If it was stupid and wrong maybe it deserves a downvote, but I don't believe in downvoting this answer just because it 'might not work'. No harm can come of this suggestion. – JT.WK – 2010-10-05T22:43:30.827

4There's always midnight commander (or other curses-based file managers) for those without X. – thomasrutter – 2010-10-07T02:39:47.277

9

If you just want to be sure:

 mv -- -rf remove.-rf
 [[check]]
 rm remove.-rf

Jan

Posted 2010-10-05T22:06:31.907

Reputation: 241

4

Just in case you are on some non-GNU Unix where you feel yourself yanked back to the stone age (no -- syntax, no -inum switch to find, no unlink command, your editor refuses editing directories etc. etc.) you can still help yourself:

find . -name '-rf' -print | xargs rm -i

This will cause find to feed all potential candidates to rm which in turn will ask for permission/denial for every single file it is fed.

In case your rm doesn't even support the -i switch (HP-UX 10.2 on PA-RISC 1.1 anyone?), just be more careful:

find . -name '-rf' -print
# check that find's output is exactly and only what you need to delete
find . -name '-rf' -print | xargs rm

Olfan

Posted 2010-10-05T22:06:31.907

Reputation: 429

what if your system doesn't even have find? – Lie Ryan – 2010-10-06T11:32:03.007

That would be evil indeed. Alas, I have yet to meet a Unix installation without one. Can you give me a pointer? – Olfan – 2010-10-06T13:44:03.363

Giving it some thought, my answer would have been more appropriate were the question asked on serverfault. Super Users are not quite probable to have machinery around as ancient as the ones I'm referring to. And those who do will have learned to help themselves. ;-) – Olfan – 2010-10-06T13:49:46.137

+1, @Olfan, in you defense, someone just now encountering a legacy unix in a closet somewhere still needs a resource to learn tips and tricks from... – RBerteig – 2010-10-06T20:51:07.533

3

Supporting jleedev's answer, I'd improve with:

rm -i -- -rf

to be in interactive mode and ask for confirmation, so you can really be sure of what you delete. (though the solution is fine. it's simply for your peace of mind)

Actually, even use that:

\rm -i -- -rf

to be sure that you're not using any aliases.

haylem

Posted 2010-10-05T22:06:31.907

Reputation: 124

2

As it has already been suggested I've always used the syntax

rm -rf -- filename

when I had to remove a file with a dash as prefix because the -- says to the command that it does not search for any other parameter but just file names.

Keeping it in mind, in order to protect my important folder by accidental file deletion I was used to create an empty file called simply -i which is normally put at the top of the file list when resolving the * search. So the command

rm -rf *

when excuted on my protected folder is exploded, dureing execution, in the command:

rm -rf -i filename1 filename2 .... (all the other files in the folder)

and the shell, instead of deleting everything immediately, stops asking for a confirmation (as the -i option requires).

Ghidello

Posted 2010-10-05T22:06:31.907

Reputation: 121

+1 for the -i filename hack - "I" was skeptical, but it tests out! – rymo – 2010-11-01T21:33:42.680

2

Last time I had this problem, I solved it with:

python

import os

os.remove("-rf")

Jono

Posted 2010-10-05T22:06:31.907

Reputation: 21

1

martin clayton is right.

It is very simple and logical. If the options exist it is always -XXX or --CCCC so if you put a ./ or the full path -rf cannot be considered as an option and will be considered as a normal string.

It works with "all" strange file names.

#pwd
/tmp/TEST
#touch ./-rf
#ls 
-rf
#rm ./-rf 
#ls
#

Louis

Posted 2010-10-05T22:06:31.907

Reputation: 2 074

1

rm -- -rf most gnu tools accept -- as marker of end of options

Konrads

Posted 2010-10-05T22:06:31.907

Reputation: 105