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.
Answer
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"
Attribution
Source : Link , Question Author : MYV , Answer Author : slhck