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

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?

Asked By: Astra

||

Use “–” to make rm stop parsing command line options, like this:

rm -- --help
Answered By: Vegar Nilsen

Or you can do

rm ./--help
Answered By: edfuh

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

Answered By: Baldur

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.

Answered By: aculich

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

Answered By: Stephen Menasian

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

Answered By: Marcin

A brutal solution:

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

perl -e "rmdir '-d' or die 'Could not rmdir.';"
Answered By: Thomas L Holaday

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.

Answered By: Eric Leschinski

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

Answered By: atti

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.