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).
34Some nerve trying to
rm
a file called-rf
from your system without remembering the exact syntax! – ChristopheD – 2010-10-05T22:08:58.2203To 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'sargc
andargv
) Thus, both of those result in the array["rm", "-rf"]
being passed torm
, which does what you would expect. – Thanatos – 2010-10-05T22:10:05.5903
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.84312An 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 forls
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