How do I delete a file whose name begins with "-" (hyphen a.k.a. dash or minus)?

193,001

Solution 1

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

rm -- --help

Solution 2

Or you can do

rm ./--help

Solution 3

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.

Solution 4

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

Solution 5

A brutal solution:

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

perl -e "rmdir '-d' or die 'Could not rmdir.';"
Share:
193,001
Astra
Author by

Astra

I write code.

Updated on September 17, 2022

Comments

  • Astra
    Astra almost 2 years

    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?

    • Admin
      Admin almost 14 years
      Would be great if this question was renamed to "How to delete a file whose name starts with --".
    • Admin
      Admin almost 14 years
      @Sandy Agreed; I normally dislike changing a question's meaning, but in this case the accepted answer is specific to this problem
    • Admin
      Admin almost 14 years
      i find it a bit ironic that rm --help actually explains how to delete filenames beginning with a -. good question nevertheless.
    • Admin
      Admin over 12 years
      @lesmana irony indeed :D. I think there is a good lesson to be learned here (read the help - it may indeed be helpful).
    • Admin
      Admin over 8 years
      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.
    • Admin
      Admin about 7 years
      Same on stackoverflow and serverfault.
    • Admin
      Admin over 3 years
      Ironically, --help isn't always helpful. OS X: rm --help returns rm: illegal option -- - and usage: rm [-f | -i] [-dPRrvW] file .... BusyBox returns Usage: rm [-irf] FILE.... The rm -- $file trick is mentioned in the man page on OS X, but most embedded systems don't bother installing the man pages.
  • Astra
    Astra almost 14 years
    I knew it was something very simple like this...
  • user603
    user603 almost 14 years
    This is the one that I remember. Using "rm -- --help" is something I always have to look up.
  • aculich
    aculich over 12 years
    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
  • Keith Thompson
    Keith Thompson over 12 years
    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
    Keith Thompson over 12 years
    Do all versions of the rm command support the -- argument?
  • jw013
    jw013 over 12 years
    @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
    jw013 over 12 years
    Making a copy of the file is not a very efficient solution for renaming it.
  • jw013
    jw013 over 12 years
    This method always works even for commands that don't treat -- specially.
  • Stephen Menasian
    Stephen Menasian over 12 years
    You could use vidir; but that is probably just as (or even more) inefficient - and much more dangerous.
  • Preeyah
    Preeyah about 12 years
    One more issue with this command is that there must be a space between {} and \;, otherwise it won't work.
  • Oscalation
    Oscalation almost 12 years
    The OP wants a command line (CLI) solution.
  • Colin
    Colin about 11 years
    @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)
  • daviewales
    daviewales almost 11 years
    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
  • raskhadafi
    raskhadafi over 10 years
    THANKS! That saved my day. And of course it works on BSD (OSX) and with other commands!
  • Hugo
    Hugo over 9 years
    How is apt-get install mc || yum install mc; mc various arrow keys and F8 easier than rm ./--help?
  • Sean
    Sean over 9 years
    It works with IBM AIX's old school System V UNIX coreutils. It is safe to say it will work anywhere.
  • Stéphane Chazelas
    Stéphane Chazelas about 9 years
    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...
  • Peter.O
    Peter.O almost 9 years
    @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)
  • dslake
    dslake over 8 years
    It works on the rm that's included with BusyBox, found on IoT devices (Raspberry Pi, Intel Edison, etc), Android phones, tablets, and other devices like Amazon Fire and even hacked Wii systems.
  • WAF
    WAF about 8 years
    Is there any downside to always using rm [options] -- [file] in environments that allow it?
  • Elephant 03
    Elephant 03 almost 7 years
    you sir, are a savior
  • StockB
    StockB over 6 years
    This works when a file begins with a single dash, where rm -- <filename> did not.
  • Kusalananda
    Kusalananda about 6 years
    I believe this was already covered in an earlier answer.
  • muenalan
    muenalan about 6 years
    Nice solution, help also with: mv ./--help /tmp/
  • John Jiang
    John Jiang almost 6 years
    I always wondered what the standalone -- means.
  • Iftakharul Alam
    Iftakharul Alam almost 6 years
    Works like a charm. :-)
  • Virender singh Rathore
    Virender singh Rathore over 5 years
    Yes I usually use this method.
  • felwithe
    felwithe about 4 years
    This should be the answer because it works for other commands as well.
  • ilkkachu
    ilkkachu over 2 years
    sigh, first, it's -inum and not -inode in at least the GNU and FreeBSD versions of find; second, -inum isn't magic, the file still gets passed by name to rm or unlink(), though here, using . as the start point will cause rm to get the filename ./-foo, bypassing the problem; third, you need a space between {} and \;.