Why you should not name files starting with a hyphen when using zsh

Saturday, July 6, 2024 at 12:07 PM | 1 min read

Last modified on Saturday, July 6, 2024 at 12:07 PM

#file naming, #illegal options, #macOS, #zsh

Illegal options

Photo by Kindel Media on pexels.com

Recently, for kicks, I tried to name a file starting with a hyphen (-), and the following was returned in Terminal:

touch -file touch: illegal option -- i usage: touch [-A [-][[hh]mm]SS] [-achm] [-r file] [-t [[CC]YY]MMDDhhmm[.SS]] [-d YYYY-MM-DDThh:mm:SS[.frac][tz]] file

However, if I try to name my file file, I am able to do so. The reason I can't name files starting with - is because of the - in one of the zsh directories. Usually the command line flags are given following a -. In the example above, -file is being treated as an illegal option for the touch command. But if I run the following command:

touch -- -file

I am able to successfully create a file named -file. Why? Because the -- indicates that -file is not a flag. However, I do not recommend naming files (or folders) that start with a hyphen (-)!