tar cvf or tar -cvf ?
Solution 1
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
.
Solution 2
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.
Solution 3
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.
Robert Iagar
Updated on September 18, 2022Comments
-
Robert Iagar almost 2 years
I have learned to use tar without '-' for options, like
tar cvfz dir.tar.gz Directory/
but I recently came accross the slightly differenttar -czvf
syntax (I think the 'f' must be the last option in this case).Both work on linux and Mac OS. Is there a recommended syntax, with ou without '-' which is more portable accross unix flavors ?
-
JoL over 6 yearsThe reason why
-f
must be the last option is because it takes an argument and that argument may be given touching the option.tar -cvfz dir.tar.gz Directory
means to create an archive namedz
that contains the filesdir.tar.gz
andDirectory
.
-
-
David Costa over 12 yearsI agree with your answer, even if the info page of tar (which is very clear about the three styles of specifying options) tells that old options are kept only for compatibility and there are a bunch of options that have NO old correspondence.
-
Richard Hansen over 12 years
-
schily almost 9 yearsAlso note that if a Linux distro contains
pax
, it usually comes with the worst known pax implementation (GNU pax) that should be avoided because of many bugs. -
schily almost 9 yearsYou are wrong: tar on AT&T UNIX was written not to have a - but it accepts the - and skips it. The only portable way to call tar is to call it without the - and if you ever find a "tar" implementation that does not accept things without -, this is not tar.
-
Ueli Hofstetter almost 9 yearsAlso, the
ps
command has the same behavior, you may omit the-
for the options. -
Gilles 'SO- stop being evil' almost 9 years@perror That's wrong. Many versions of
ps
don't let you omit the-
. The Linux version has options with and without-
, but most have different meanings (BSD vs System V compatibility). -
Ueli Hofstetter almost 9 yearsIt is true that, most of the time, I only use
ps aux
... So, I might be (partly) wrong. Sorry. -
Software_Programineer over 7 yearsSo going forward, and to plainly answer Gilles question. Yes, the recommended, now standardized, and good practice format now would include the - before the options whether its needed or not.