Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question

marked as duplicate by lesmana, rahmu, slm, Anthon, jasonwryan Aug 25 '13 at 18:16

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

4 Answers 4

up vote 20 down vote accepted

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.

share|improve this answer
    
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 '<space><tab><newline>'. 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 7:34
1  
@fered Nice! I tried it, and sure enough: it doesn't affect it. Here is $IFS in SUS: pubs.opengroup.org/onlinepubs/9699919799/utilities/… So I went looking for documentation about what does affect arguments, and found this: pubs.opengroup.org/onlinepubs/9699919799/utilities/… . 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

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.

share|improve this answer

You can also use:

echo -n > -stuff
share|improve this answer
    
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 6: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..

share|improve this answer
2  
cp -- /dev/null -a is overkill, : > -a or even > -a are sufficient. –  jlliagre Oct 19 '11 at 5:38

Not the answer you're looking for? Browse other questions tagged or ask your own question.