How do I delete a file whose name begins with "-" (hyphen a.k.a. dash or minus)?
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.';"
Comments
-
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 almost 14 yearsWould be great if this question was renamed to "How to delete a file whose name starts with --".
-
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 almost 14 yearsi find it a bit ironic that
rm --help
actually explains how to delete filenames beginning with a-
. good question nevertheless. -
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 over 8 yearsI 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 about 7 yearsSame on stackoverflow and serverfault.
-
Admin over 3 yearsIronically,
--help
isn't always helpful. OS X:rm --help
returnsrm: illegal option -- -
andusage: rm [-f | -i] [-dPRrvW] file ...
. BusyBox returnsUsage: rm [-irf] FILE...
. Therm -- $file
trick is mentioned in the man page on OS X, but most embedded systems don't bother installing the man pages.
-
-
Astra almost 14 yearsI knew it was something very simple like this...
-
user603 almost 14 yearsThis is the one that I remember. Using "rm -- --help" is something I always have to look up.
-
aculich over 12 yearsThis 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 over 12 yearsI 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 userm ./--help
(orrm -i *
. -
Keith Thompson over 12 yearsDo all versions of the
rm
command support the--
argument? -
jw013 over 12 years@KeithThompson
find
prefixes the command line argument path to all files, so it would runrm ./--help
andrm ./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 over 12 yearsMaking a copy of the file is not a very efficient solution for renaming it.
-
jw013 over 12 yearsThis method always works even for commands that don't treat
--
specially. -
Stephen Menasian over 12 yearsYou could use vidir; but that is probably just as (or even more) inefficient - and much more dangerous.
-
Preeyah about 12 yearsOne more issue with this command is that there must be a space between
{}
and\;
, otherwise it won't work. -
Oscalation almost 12 yearsThe OP wants a command line (CLI) solution.
-
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 almost 11 yearsMidnight 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 over 10 yearsTHANKS! That saved my day. And of course it works on BSD (OSX) and with other commands!
-
Hugo over 9 yearsHow is
apt-get install mc || yum install mc; mc
various arrow keys and F8 easier thanrm ./--help
? -
Sean over 9 yearsIt works with IBM AIX's old school System V UNIX coreutils. It is safe to say it will work anywhere.
-
Stéphane Chazelas about 9 yearsAnd it also works to avoid special treatment by some commands for other types (beside options) of specially named arguments like the
-
of text utilities orcd
, thefoo=bar
ofawk
... -
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 over 8 yearsIt 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 about 8 yearsIs there any downside to always using
rm [options] -- [file]
in environments that allow it? -
Elephant 03 almost 7 yearsyou sir, are a savior
-
StockB over 6 yearsThis works when a file begins with a single dash, where
rm -- <filename>
did not. -
Kusalananda about 6 yearsI believe this was already covered in an earlier answer.
-
muenalan about 6 yearsNice solution, help also with: mv ./--help /tmp/
-
John Jiang almost 6 yearsI always wondered what the standalone -- means.
-
Iftakharul Alam almost 6 yearsWorks like a charm. :-)
-
Virender singh Rathore over 5 yearsYes I usually use this method.
-
felwithe about 4 yearsThis should be the answer because it works for other commands as well.
-
ilkkachu over 2 yearssigh, 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 torm
orunlink()
, though here, using.
as the start point will causerm
to get the filename./-foo
, bypassing the problem; third, you need a space between{}
and\;
.