tar cvf or tar -cvf ?
tar
is one of those ancient commands from the days when option syntax hadn't been standardized. Because all useful invocations of tar
require specifying an operation before providing any file name, most tar
implementations interpret their first argument as an option even if it doesn't begin with a -
. Most current implementations accept a -
; the only exception that I'm aware of is Minix.
Older versions of POSIX and Single Unix included a tar
command with no -
before the operation specifier. Single Unix v2 had both traditional archivers cpio
and tar
, but very few flags could be standardized because existing implementations were too different, so the standards introduced a new command, pax
, which is the only standard archiver in since Single Unix v3. If you want standard compliance, use pax
, but beware that many Linux distributions don't include it in their base installation, and there's no pax
in Minix. If you want portability in practice, use tar cf filename.tar
.
I may be a dinosaur, but I think that habitually using cvf
instead of -cvf
is probably more portable. I imagine most Linux distros use GNU tar, and I would guess that the *BSDs do also, but you'll find proprietary Unixes that still use the old SysV tar, which used to require you to not use a -
in the options.
I do not use -cvf
(or -xf
or whatever) and I have no trouble even with bleeding edge Arch linux.
And just as a side note, I think you can use the Sun-standard jar
command options with or without a -
as well.
There have been few programs on UNIX that do not follow the current option standard.
One is dd
, but dd was derived from the IBM mainframe program DDR
(Disk Dump and Restore).
One is ar
and the other is tar
. From my information, tar wanted to be similar to ar.
All tar implementations work without the - and no useful implementation requires the -. So if you like to write portable scripts, check the SUSv2 standard and only use a commandline that is compatible to SUSv2.