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