How to remove files starting with double hyphen?
Asked Answered
T

7

76

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?

Thuja answered 1/4, 2009 at 15:57 Comment(2)
Same on serverfault and unix.Homogeny
@Homogeny Well ok, but I was first :)Thuja
S
153
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

Seaport answered 1/4, 2009 at 15:59 Comment(1)
This is not standard across all versions of Unix, though it does work on most. See vatine's answer for an alternative that works with all versions.Panier
A
56
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).

Armalda answered 1/4, 2009 at 19:10 Comment(1)
+1 because you're right, but making scripts bulletproof is a whole other issue besides dealing with one awkward filename. If the question was "how do I removed files starting with hyphens or containing spaces, ;, <>, $, and other malicious junk" then it would need the full answer :-)Seaport
A
18

Either rm -- --testings.html or rm ./--testings.html.

Atchison answered 1/4, 2009 at 16:1 Comment(0)
S
7
rm -- --testings.html
Socalled answered 1/4, 2009 at 15:59 Comment(0)
M
4

Yet another way to do it is to use find ... -name "--*" -delete

touch -- --file 
find -x . -mindepth 1 -maxdepth 1 -name "--*" -delete 
Muntin answered 2/4, 2009 at 10:43 Comment(0)
A
3

For a more generalised approach for deleting files with impossible characters in the filename, one option is to use the inode of the file.

It can be obtained via ls -i.

e.g.

$ ls -lai | grep -i test
452998712 -rw-r--r--  1 dim   dim      6 2009-05-22 21:50 --testings.html

And to erase it, with the help of find:

$ find ./ -inum 452998712 -exec rm \{\} \;

This process can be beneficial when dealing with lots of files with filename peculiarities, as it can be easily scripted.

Apologize answered 22/5, 2009 at 18:56 Comment(1)
That works only because it invokes rm with the argument ./--testings.html. It's easier just to type rm ./--testings.html directly -- which also doesn't need to traverse the entire directory tree. (You could avoid the latter by using -maxdepth 1.)Clatter
C
2
rm ./--testings.html

or

rm -- --testings.html
Capri answered 1/4, 2009 at 16:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.