LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-23-2006, 07:39 AM   #1
Flesym
Member
 
Registered: Aug 2005
Location: Germany
Distribution: Ubuntu, Debian
Posts: 189

Rep: Reputation: 31
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
 
Old 01-23-2006, 07:47 AM   #2
scuzzman
Senior Member
 
Registered: May 2004
Location: Hilliard, Ohio, USA
Distribution: Slackware, Kubuntu
Posts: 1,851

Rep: Reputation: 47
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
 
Old 01-23-2006, 07:49 AM   #3
Flesym
Member
 
Registered: Aug 2005
Location: Germany
Distribution: Ubuntu, Debian
Posts: 189

Original Poster
Rep: Reputation: 31
Ah yes, of course! -Thank you!
 
Old 01-23-2006, 08:01 AM   #4
Dtsazza
Member
 
Registered: Oct 2005
Location: Oxford, UK
Distribution: Debian Etch (w/ dual-boot XP for gaming)
Posts: 282

Rep: Reputation: 31
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: A Libertarian begins to see the light LXer Syndicated Linux News 0 01-09-2006 04:31 PM
Partition begins after end-of-disk cornleader Linux - Hardware 3 04-15-2005 10:45 AM
Tried to delete file as root but it says I don't have permission to delete it! beejayzed Mandriva 23 03-12-2004 03:46 AM
Where the kernel begins...? Oxyd Mandriva 2 10-30-2003 05:14 AM
Delete contents of the file, but not file? mikeshn Linux - General 9 05-28-2003 07:47 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:25 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration