5

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.

4 Answers4

20

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.

Shawn J. Goff
  • 46,081
  • it means I can write like this touch --stuff – Elham abbasi Oct 17 '11 at 23:24
  • 5
    @Elham-abbasi, no; -- 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:27
  • 2
    The IFS does not apply to the delimiter between positional parameters. Rather, as per man 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 ''.  eg. 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
  • 1
    @fered Nice! I tried it, and sure enough: it doesn't affect it. Here is $IFS in SUS: http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_05

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

    – Shawn J. Goff Oct 18 '11 at 11:14
15

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.

greenoldman
  • 6,176
4

You can also use:

echo -n > -stuff
Michael Mrozek
  • 93,103
  • 40
  • 240
  • 233
2

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

Peter.O
  • 32,916