Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove files starting with double hyphen?

I have some files on my Unix machine that start with

 -- 

e.g. --testings.html

If I try to remove it I get the following error:

cb0$ rm --testings.html rm: illegal option -- - usage: rm [-f | -i] [-dPRrvW] file ...        unlink file 

I tried

rm "--testings.html" || rm '--testings.html'  

but nothing works.

How can I remove such files on terminal?

like image 909
cb0 Avatar asked Apr 01 '09 15:04

cb0


People also ask

How do I delete a directory that starts with a dash?

You can use standard UNIX or Linux rm command to delete a file name starting with - or -- . All you have to do is instruct the rm command not to follow end of command line flags by passing double dash -- option before -foo file name.

How do I remove a file with the name '- something?

How do I remove or access a file with the name '-something' or containing another strange character ? If your file starts with a minus, use the -- flag to rm; if your file is named -g, then your rm command would look like rm -- -g.

Which command is used for removing file named?

The rm command is used to delete files.

How do I delete an argument list too long?

To resolve this issue and delete all files use xargs command-line utility with the find command. Then execute command to delete all files in current directory and its sub directories. WARNING – This will also delete files from subdirectories. Be careful with this command.


2 Answers

rm -- --testings.html 

The -- option tells rm to treat all further arguments as file names, not as options, even if they start with -.

This isn't particular to the rm command. The getopt function implements it, and many (all?) UNIX-style commands treat it the same way: -- terminates option processing, and anything after it is a regular argument.

http://www.gnu.org/software/hello/manual/libc/Using-Getopt.html#Using-Getopt

like image 121
Steve Jessop Avatar answered Oct 06 '22 07:10

Steve Jessop


rm -- --somefile 

While that works, it's a solution that relies on rm using getopts for parsing its options. There are applications out there that do their own parsing and will puke on that too (because they might not necessarily implement the "-- means end of options" logic).

Because of that, the solution you should drive through your skull is this one:

rm ./--somefile 

It will always work, because this way your arguments never begin with a -.

Moreover, if you're trying to make really decent shell scripts; you should technically be putting ./ in front of all your filename parameter expansions to prevent your scripts from breaking due to funky filename input (or to prevent them being abused/exploited to do things they're not supposed to do: for instance, rm will delete files but skip over directories; while rm -rf * will delete everything. Passing a filename of "-rf" to a script or somebody touch ~victim/-rf'ing could in this way be used to change its behaviour with really bad consequences).

like image 44
lhunath Avatar answered Oct 06 '22 09:10

lhunath