I was wondering how to make a file named as a flag, e.g., if I wanted to make a folder named -a
, what commands would do the trick?
I tried mkdir '-a'
, mkdir \-a
, and neither worked. I'm on Ubuntu.
I was wondering how to make a file named as a flag, e.g., if I wanted to make a folder named -a
, what commands would do the trick?
I tried mkdir '-a'
, mkdir \-a
, and neither worked. I'm on Ubuntu.
Call the command like so:
mkdir -- -a
The --
means that the options end after that, so the -a
gets interpreted literally and not as an option to mkdir
. You will find this syntax not only in mkdir
, but any POSIX-compliant utility except for echo
and test
. From the specification:
The argument -- should be accepted as a delimiter indicating the end of options. Any following arguments should be treated as operands, even if they begin with the '-' character. The -- argument should not be used as an option or as an operand.
Using --
as a safeguard is recommended for almost any action where you deal with filenames and want to make sure they don't break the command, e.g. when moving files in a loop you might want to call the following, so that a file called -i
isn't (in?)correctly parsed as an option:
mv -- "$f" new-"$f"
cc -o -myfile -Wall -myfile.c
as an only slightly contrived example. Any editor worth its salt should be able to save to a file named -myfile.c
if you ask that of it, but the C compiler will probably not do what you want when given such a list of options.
cc
command Michael gave above, which has the -o
option to specify the output file.
for f in ./*; do...
rather than for f in *; do...
The simplest way that should work with any reasonable program is to use a relative path name in front of the -
, e.g. mkdir ./-a
will create a directory called -a
in the current working directory.
The most common example of using this "trick" is when you want to remove a file which begins with a dash, so you can do rm ./-a
.
./
is always a good idea—imagine you created a file called -rf
and then you called rm *
.
--
is not understood by every commands, just a few)
May 27, 2013 at 12:11
--
was a bash thing, so it should work for all programs?
The reason why mkdir '-a'
, or mkdir \-a
doesn't work is because both of these methods (using quotes or backslashes) are used to prevent your shell (likely bash
) from giving them any special meaning. Since -a
doesn't mean anything special to the shell anyway, these have no effect on how it gets passed on to mkdir
.
As far as mkdir
is concerned, it receives a list of arguments from the shell, and it can't tell whether you put them in quotes or not, or whether there was a backslash. It just sees one argument -a
. That's why you need the --
as explained in the other answers.
In addition to the answer from "slhck" there is another trick that sometimes works:
Put the filename in question within 2 sets of (different) qoutes like "'-a'"
or '"-a"'
.
Whether or not this works depends on the program, but it is a last resort if the program doesn't support the POSIX --
feature.
The POSIX method is always preferred as it is 100% reliable.
--
to terminate options processing also assumes that you don't need any options to follow the file name, which is not always the case.
'-a'
or "-a"
, at least on BSD-type systems
May 26, 2013 at 15:27