Last updated on July 12, 2020 by Dan Nanni
Some Linux tools generate file with leading dash or hyphen (-
) in filenames. Or your hand-written program can accidentally produce filenames starting with minus. If you attempt to remove, copy, rename, or otherwise access such files by using ls
, rm
, cp
, mv
or chmod
, you will get the following errors.
[cmd]: invalid option --
That is because your Linux system interprets the leading minus in filename as part of command line option, and throws an invalid option or non-existent mode error.
In order to deal with a leading dash in filename under Linux shell, you can try one of the following two methods.
Since a leading minus in filename introduces ambiguity in recognizing the filename, you just need to eliminate such ambiguity by prepending a pathname to the filename. For example, if you have a file named -my.txt
in current directory, you can access it as follows.
$ chmod 600 ./-my.txt $ rm ./-my.txt
The second method to deal with a leading hyphen character is to take advantage of a special argument --
which is interpreted by getopt()
as the "end of option." Most standard Linux command line utilities use getopt()
to process command line arguments. When getopt()
encounters --
, it stops option-scanning process. Therefore, just insert --
in front of the filename to make it explicit that the hyphenated filename is not part of command line arguments.
$ chmod -- 600 -my.txt $ rm -- -my.txt
This website is made possible by minimal ads and your gracious donation via PayPal or credit card
Please note that this article is published by Xmodulo.com under a Creative Commons Attribution-ShareAlike 3.0 Unported License. If you would like to use the whole or any part of this article, you need to cite this web page at Xmodulo.com as the original source.
Xmodulo © 2021 ‒ About ‒ Write for Us ‒ Feed ‒ Powered by DigitalOcean