68

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.

djsmiley2kStaysInside
  • 6,943
  • 2
  • 36
  • 48

12 Answers12

128
unlink -rf

Or

rm -- -rf
Josh Lee
  • 1,633
61

Another option:

rm ./-rf

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

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
  • 431
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
  • 349
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
  • 20,411
9

If you just want to be sure:

 mv -- -rf remove.-rf
 [[check]]
 rm remove.-rf
Jan
  • 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
  • 449
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
  • 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
  • 121
2

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

python

import os

os.remove("-rf")

Jono
  • 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
#
RBerteig
  • 3,335
Louis
  • 2,770
1

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

Konrads
  • 135