Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

How do you remove a file whose filename begins with a dash (hyphen or minus) -? I'm ssh'd into a remote OSX server and I have this file in my directory:

tohru:~ $ ls -l
total 8
-rw-r--r--    1 me  staff  1352 Aug 18 14:33 --help
...

How in the world can I delete --help from a CLI? This issue is something that I come across in different forms on occasion, these files are easy to create, but hard to get rid of.

I have tried using backslash

rm \-\-help

I have tried quotes

rm "--help"

How do I prevent the minus (dash or hyphen) character to be interpreted as an option?

share|improve this question
1  
Would be great if this question was renamed to "How to delete a file whose name starts with --". –  Sandy Sep 2 '10 at 21:04
    
@Sandy Agreed; I normally dislike changing a question's meaning, but in this case the accepted answer is specific to this problem –  Michael Mrozek Sep 2 '10 at 21:52
14  
i find it a bit ironic that rm --help actually explains how to delete filenames beginning with a -. good question nevertheless. –  lesmana Sep 3 '10 at 6:44
    
@lesmana irony indeed :D. I think there is a good lesson to be learned here (read the help - it may indeed be helpful). –  jw013 Dec 26 '11 at 21:45
1  
@jw013 Sometimes the help is easy for experienced users to understand, but overwhelming and confusing to newbs. –  iconoclast Jun 13 '12 at 3:37

8 Answers 8

up vote 74 down vote accepted

Use "--" to make rm stop parsing command line options, like this:

rm -- --help
share|improve this answer
    
I knew it was something very simple like this... –  Bryan Rehbein Sep 2 '10 at 19:37
2  
Do all versions of the rm command support the -- argument? –  Keith Thompson Dec 6 '11 at 7:18
2  
@KeithThompson -- is a feature of most GNU tools, so it won't work on most non-GNU ("non-Linux") Unix'es (e.g. BSD variants or some embedded systems) –  dtech Apr 6 '13 at 17:00
    
THANKS! That saved my day. And of course it works on BSD (OSX) and with other commands! –  raskhadafi Dec 2 '13 at 8:46

Linux Walkthrough of creating a file with dashes and spaces, then removing it.

BE CAREFUL! Don't accidentally run a rm -rf / or similar cascade delete command.

If your file you are trying to remove includes asterisks or slashes, do not accidentally pump a . or /* or * or some other wildcard which could cascade delete your operating system.

Create a file called "--yo yo"

eric@dev ~ $ touch -- "--yo yo"
eric@dev ~ $ ls
bin  --yo yo

First, find it with find:

eric@dev ~ $ find . -name "*--yo yo*"
./--yo yo

Make sure the find command ONLY finds the ONE file you want to delete:

Then pass the -delete option to find, to delete them:

eric@dev ~ $ find . -name "*--yo yo*" -delete
eric@dev ~ $ ls
bin  

Aaaannd it's gone.

share|improve this answer

A brutal solution:

perl -e "unlink '--help' or die 'Could not unlink.';"

perl -e "rmdir '-d' or die 'Could not rmdir.';"
share|improve this answer

Midnight Commander (mc) is the easiest, just point at it and hit F8 ;)

share|improve this answer
    
The OP wants a command line (CLI) solution. –  Francesco Turco Sep 14 '12 at 13:11
    
Midnight Commander IS a CLI solution. Just use your package manger to install it. (It even works over ssh...) en.wikipedia.org/wiki/Midnight_Commander –  daviewales Sep 20 '13 at 0:41

If you want to rename the file, -.bar, (mv won't work), try this:

cat >foo.bar <-.bar

before using the command:

rm -- -.bar

You should be able to examine the original file's contents in foo.bar

share|improve this answer
2  
Making a copy of the file is not a very efficient solution for renaming it. –  jw013 Dec 26 '11 at 21:42
    
You could use vidir; but that is probably just as (or even more) inefficient - and much more dangerous. –  Stephen Menasian Dec 27 '11 at 0:22

Use find to do it:

find -name '--help' -delete

And this is a good method because if you have more then a few files like this that you can delete you can get a preview list of the files by simply running find without the -delete option first, and then if the list of files look good just run it again with -delete.

In fact, you avoiding rm in favor of find (especially with preview first) is a good habit that will help you avoid mistakes with rm * that will inevitably bite you some day.

Note, though, that find will recurse through all your subdirectories, so you might want to run it with a subdirectory depth constraint like this:

find -maxdepth 1 -name '--help' -delete

which limits the find to the current directory.

share|improve this answer

The answers of Vegar Nilsen and edfuh are very good and the proper solutions to a problem like this.

I do want to add a general response to this question that allows you to delete any file with a difficult file name. First its inode number is obtained using ls -i or some form of stat and then the file is removed by searching for files in the current directory by inode number and executing the rm command on the file with a matching inode number

find . -inode <inode> -exec rm {}\;

Since inode numbers are unique in each file system you can remove any file using this; unicode or using escape characters. It is how ever very annoying to type out so I would recommend adding the line

TAB: menu-complete             # Tab: Cycles through the command
"\e[Z": menu-complete-backward # Shift-Tab: Cycles backwards

into your .inputrc file if you're using bash. This allows you to cycle through the list of possible completions (for further information).

share|improve this answer
4  
This way has two problems: 1) do not use -exec rm when you can use -delete; 2) obtaining the inode and using that is needlessly overcomplicated when you can just use: find -name '--help' -delete –  aculich Dec 6 '11 at 1:36
    
I don't think that will work. It traverses all the files in the current directory and all its subdirectories, and after all that it still invokes rm --help, which still won't remove the file. Just use rm ./--help (or rm -i *. –  Keith Thompson Dec 6 '11 at 7:16
    
@KeithThompson find prefixes the command line argument path to all files, so it would run rm ./--help and rm ./sub/dirs/--help. To fix the second, one would have to add -maxdepth 1, but all of this is essentially applying @edfuh's solution in a more roundabout, convoluted way, and -delete is safer than -exec rm anyways. –  jw013 Dec 26 '11 at 21:39
    
One more issue with this command is that there must be a space between {} and \;, otherwise it won't work. –  Eugene S May 22 '12 at 14:08

Or you can do

rm ./--help
share|improve this answer
5  
Not that it matters but this seems the most "proper". –  James M. Sep 6 '10 at 9:10
    
This is the one that I remember. Using "rm -- --help" is something I always have to look up. –  user603 Sep 21 '10 at 4:12
4  
This method always works even for commands that don't treat -- specially. –  jw013 Dec 26 '11 at 21:47

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.