LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to delete a file that begins with a '-' ? (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-delete-a-file-that-begins-with-a-406884/)

Flesym 01-23-2006 06:39 AM

How to delete a file that begins with a '-' ?
 
Hi,

actually this is not really a serious current problem of mine, but I would really like to know the proper way. Here's the situation: Yesterday I downloaded a file which name began with a dash ('-'). And after I got the feeling that I didn't want this file any more, I tried to delete it and recognized, that this was a much tougher task than I thought: The original name was quite long so let's abstract a little bit and assume it was called '-bla'. I think I'm quite familiar with linux command line and bash but this is going to drive me crazy. My first try was:

$ rm -bla

of course this didn't work and I recognized it yet before I pressed enter. And then the trouble began..., I tried all of these:

Code:

$ rm "-bla"
$ rm \-bla
$ rm "\-bla"
$ rm ?bla
$ rm [-]bla
$ rm [\-]bla
$ rm *bla

... and I guess even some more, but none of them worked. I'm sure there is a vary bold solution that just doesn't come in my mind, so if you have an idea let me know. Btw, graphical file-browsers like nautilus don't have any problem with this, they create and delete such files like a breeze..., but how?

-flesym

scuzzman 01-23-2006 06:47 AM

From this page:
Quote:

Q: How can I delete a file beginning with a hyphen?

A: Because a hyphen at the beginning of a filename will be taken to be
an option of the rm (remove/delete) command, this causes a special
problem when you try to delete (or copy or rename) it. You will
probably get an error message telling you that there is no such option.
One way to delete the file would be to specify the current directory
along with the filename so that the filename no longer begins with a
hyphen. Do this by putting the characters ./ (which specifies the
current directory) before the filename, as in

rm ./-badname

Flesym 01-23-2006 06:49 AM

Ah yes, of course! -Thank you!

Dtsazza 01-23-2006 07:01 AM

Another method (just for the sake of completeness) is to specify give the argument "--" before the name of the file. This tells the shell that at this point, all arguments have been given, so it stops parsing dashes as argument switches (something along these lines, it may not be technically accurate). For example, you could remove a file called -foo with
Code:

rm -- -foo
as well as with the perfectly adequate
Code:

rm ./-foo


All times are GMT -5. The time now is 07:03 PM.