28

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.

Jens Erat
  • 18,485
  • 14
  • 68
  • 80
MYV
  • 1,243

4 Answers4

48

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"
slhck
  • 235,242
21

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.

slhck
  • 235,242
4

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.

hammar
  • 271
-3

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.

Tonny
  • 33,276