2008-04-02

UNIX Tip: Double Dashes

Files that start with a dash can cause problems if you don't know how to deal with them. Let's take a look:

Chimera:Documents axon$ ls -1
-file.doc
2008-03-30.mp3
BitPIM.dmg
H-i-R.xcf
Parts.odt
vCards
The file "-file.doc" will cause problems with most command-line tools because they think -file.doc is an argument, not a file name.
Chimera:Documents axon$ cat -file.doc
cat: illegal option -- f
usage: cat [-benstuv] [file ...]
Chimera:Documents axon$ rm -file.doc
rm: illegal option -- l
usage: rm [-f | -i] [-dPRrvW] file ...
unlink file
Chimera:Documents axon$ mv -file.doc file.doc
mv: illegal option -- l
usage: mv [-f | -i | -n] [-v] source target
mv [-f | -i | -n] [-v] source ... directory
See what I mean?

Placing two dashes anywhere in the argument list of almost every shell utility will tell that utility that all of the optional arguments have been passed and that anything following the double dash is to be taken literally -- usually this means it's a filename*. This lets you manipulate files that would otherwise cause you problems.
Chimera:Documents axon$ cat -- -file.doc 
this is a test file.
Chimera:Documents axon$ mv -- -file.doc file.doc
Chimera:Documents axon$ ls -1
2008-03-30.mp3
BitPIM.dmg
H-i-R.xcf
Parts.odt
file.doc
vCards
For clarity, I used ls -1 (the number one not the lowercase letter "L" ) to force output to one filename per line.

* For certain things, the literal arguments are passed on to a separate utility or script. A good example of this: Most startx scripts process command-line arguments, but you can use -- to pass additional arguments directly to the X Server, untouched by the startx script.

blog comments powered by Disqus