tar's unusual argument handling

Tim Kientzle kientzle at acm.org
Fri Jan 9 14:20:52 PST 2004


Markus Brueffer wondered why:
    tar xvfj file.tar.bz2
is not the same as
    tar -xvfj file.tar.bz2

> What I'm asking me, is why the "-" makes a difference, though I haven't looked 
> at the sources, yet. The manpage states, that the "-" is only optional, so 
> "tar -jxfv" and "tar jxvf" should be equivalent, but obviously they are not.

The manpage has not explained the history here.

The original tar programs (1970s?) used a very peculiar
command line:
     tar [letter options] [arguments to those options]

For example, the 'b' and 'f' options both require
arguments, so you would use them as
     tar xbfv 32 file.tar

Note that '32' is the argument to 'b' and 'file.tar'
is the argument to 'f'.  Also note that there
is no '-' here.

This is totally different from other Unix programs,
of course, so most current tar programs handle the
arguments differently depending on whether there
is a leading '-'.  If there is a leading '-', then
it uses common Unix conventions, so that
    tar -xfj file.tar.bz2
has 'j' as the argument to '-f', but
    tar xfj file.tar.bz2
considers 'file.tar.bz2' to be the argument to 'f'.
To avoid this confusion, always put the 'f' last:
    tar xjf file.tar.bz2
and
    tar -xjf file.tar.bz2
really do mean the same thing.

It's also worth noting that for many tar programs,
the first argument MUST BE the mode argument ('x', 'c', 't',
'r', 'u', and sometimes 'd' or 'A'), that is
     tar xvjf file.tar.bz2      GOOD
but
     tar jxvf file.tar.bz2      BAD

My 'bsdtar' implementation, in particular, requires
that the first option be a mode specifier (because
it allows it to provide better feedback about nonsense
options and handles some odd cases where options
don't have quite the same meaning in different modes).

Tim



More information about the freebsd-hackers mailing list