455

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?

8
  • 1
    Would be great if this question was renamed to "How to delete a file whose name starts with --".
    – Sandy
    Sep 2, 2010 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 Sep 2, 2010 at 21:52
  • 43
    i find it a bit ironic that rm --help actually explains how to delete filenames beginning with a -. good question nevertheless.
    – Lesmana
    Sep 3, 2010 at 6:44
  • 3
    @jw013 Sometimes the help is easy for experienced users to understand, but overwhelming and confusing to newbs.
    – iconoclast
    Jun 13, 2012 at 3:37
  • 3
    I ran into this on a system using rm from BusyBox. Everything is minimal, including the help, so rm --help did not provide any clues.
    – dslake
    Mar 19, 2016 at 23:35

10 Answers 10

675

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

rm -- --help
11
  • 1
    I knew it was something very simple like this...
    – Astra
    Sep 2, 2010 at 19:37
  • 2
    Do all versions of the rm command support the -- argument? Dec 6, 2011 at 7:18
  • 3
    @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, 2013 at 17:00
  • 1
    THANKS! That saved my day. And of course it works on BSD (OSX) and with other commands!
    – raskhadafi
    Dec 2, 2013 at 8:46
  • 8
    @dtech, getopt() and -- predate GNU (SysIII, 1980) and are standard/POSIX. Standard utilities except for a few exceptions (like echo) understand --. YMMV for other commands if they don't use the getopt() API to parse options. Jun 17, 2015 at 12:08
276

Or you can do

rm ./--help
7
  • 4
    This is the one that I remember. Using "rm -- --help" is something I always have to look up.
    – user603
    Sep 21, 2010 at 4:12
  • 32
    This method always works even for commands that don't treat -- specially.
    – jw013
    Dec 26, 2011 at 21:47
  • And it also works to avoid special treatment by some commands for other types (beside options) of specially named arguments like the - of text utilities or cd, the foo=bar of awk... Jun 17, 2015 at 10:31
  • 1
    This works when a file begins with a single dash, where rm -- <filename> did not.
    – StockB
    Oct 31, 2017 at 14:31
  • Nice solution, help also with: mv ./--help /tmp/
    – muenalan
    Apr 21, 2018 at 5:52
47

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.

1
  • Portably, you need to give find a list of files/dirs to look into. So find . -name.... -delete and -maxdepth are not standard options either. Jun 17, 2015 at 12:17
20

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 or files with a matching inode number:

find . -inum <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).

6
  • 6
    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, 2011 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 *. Dec 6, 2011 at 7:16
  • 1
    @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, 2011 at 21:39
  • 2
    One more issue with this command is that there must be a space between {} and \;, otherwise it won't work.
    – Eugene S
    May 22, 2012 at 14:08
  • inodes may be unique but there may be several directory entries with the same inode. Those are called hardlinks. And they're only unique per file system, so you'd need to use -xdev. That won't help you to run that ls -i --help. Jun 17, 2015 at 12:16
11

A brutal solution:

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

perl -e "rmdir '-d' or die 'Could not rmdir.';"
8

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.

2

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

4
  • 3
    The OP wants a command line (CLI) solution.
    – user22304
    Sep 14, 2012 at 13:11
  • 1
    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, 2013 at 0:41
  • 6
    How is apt-get install mc || yum install mc; mc various arrow keys and F8 easier than rm ./--help?
    – Josh
    Dec 11, 2014 at 16:33
  • 4
    @daviewales: Midnight Commander is started from the command line, and it runs in the terminal, but all keyboard actions within Midnight Commander are not from the CLI (Command Line Interface) - they are from Midnight Commander's Interface. - Typically, command line utilities can be run within a script (an exception is bash command line history)
    – Peter.O
    Aug 17, 2015 at 21:33
1

here's a solution that i had used before finding this thread.
use vim to 'edit' directory:

vim .

then (in vim) select your file, hit del and confirm deletion.
when you're done quit vim with :q

-1

Have you tried to add the directory name as prefix:

$ rm ./-filename.txt dirname/-filename2.txt
$ mv ./-filename.txt filename.txt
$ cp teste ./-teste

Using the directory as prefix of the file in general helps avoiding the wrongly interpretation of the "minus" character as a command option by the parser function.

1
-4

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

2
  • 5
    Making a copy of the file is not a very efficient solution for renaming it.
    – jw013
    Dec 26, 2011 at 21:42
  • 1
    You could use vidir; but that is probably just as (or even more) inefficient - and much more dangerous. Dec 27, 2011 at 0:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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