How can we create a empty file with the unix name -stuff
.
It means the name start with -
. I tried with touch -stuff
but it didn't work.

- 829,060

- 743
4 Answers
In general, most utilities have options that begin with -
. Most of those utilities have a feature that allows you to specify an argument that is not an option by supplying the special option --
. For those utilities, --
means that no further arguments are options. So in your case, you can use touch -- -stuff
.
For more information about general conventions that many utilities follow, see Section 12: "Utility Conventions" of the Base Definitions Volume of the Single Unix Specification.
Another way to create an empty file is by using the shell's redirection operator like so: > -stuff
.

- 46,081
Also
touch ./-stuff
What happens here: for "usual" filenames the current directory (./
) is implied and thus unnecessary. However, we can explicitly specify it with ./
(it's just redundant in normal cases); this way, the argument won't start with -
and won't be parsed as an option, but as a filename.

- 6,176
You can also use:
echo -n > -stuff

- 93,103
- 40
- 240
- 233

- 39,269
-
echo -n
is not portable, you might end with a file containing "-n". Better to avoid running any command and just keep the redirection which creates (or empties) the target file. – jlliagre Oct 19 '11 at 06:42 -
@jlliagre.. Interesting, thanks... Some info on this point can be found here: echo: APPLICATION USAGE – Peter.O Oct 20 '11 at 15:00
As your question relates specifically to creating an empty file, be aware that touch
is primarily intended to "touch" the timestamp of a file.
A secondary aspect of touch
is that it will create an empty file if the named file doesn't exist.
This, of course, means that it will not always create an empty file.
To be certain that you end up with an empty file, you can use the >
redirection opera1tor (as mentioned in other answers), or cp -- /dev/null -a
touch
allow for creating (or just modifying the timestamp) of many files at once.
>
and cp
can only handle one file at a time..

- 32,916
--
must be its own separate argument, separated from the preceding and following arguments by the internal field separator (usually a space). – Shawn J. Goff Oct 17 '11 at 23:27man bash
... IFS is The Internal Field Separator that is used for word splitting after expansion and to split lines into words with the read builtin command. The default value is 'IFS=+; var1="a b c"; var2="a+b+c"; set $var1 $var2; echo "$@"; echo $1l; echo $2; echo $3; echo $4
.. for more, see Advanced Bash-Scripting Guide -$IFS and whitespace – Peter.O Oct 18 '11 at 07:34$IFS
in SUS: http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_05So I went looking for documentation about what does affect arguments, and found this: http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_03 . A
– Shawn J. Goff Oct 18 '11 at 11:14<blank>
character is what separates tokens from each other. On the same page, you find that it is defined by the locale and can be changed by$LC_CTYPE
.