4

In the GNU manual for tar, there is the following example to show that the order of the options is important:

tar -cfv collection.tar blues folk jazz

In this case, because v is placed after f, then the tar archive will be named "v", and tar will try to add collection.tar to the archive if it exists in the current directory. But if I omit the - from the beginning, then the command works as we would want it to, despite v being placed after f. Why?

1

2 Answers 2

5

Because that is the way the different option styles work and they behave differently.


The man page states for UNIX or short-option style, the one which is prefixed with a single dash.

Any number of options not taking arguments can be clustered together after a single dash, e.g. -vkp. Options that take arguments (whether mandatory or optional), can appear at the end of such a cluster, e.g. -vkpf a.tar.


The traditional style option style, the one without dashes:

In traditional style, the first argument is a cluster of option letters and all subsequent arguments supply arguments to those options that require them. The arguments are read in the same order as the option letters. Any command line words that remain after all options has been processed are treated as non-optional arguments: file or archive member names.


As you can see, the UNIX or short-option style expects arguments to options directly after the option.
Whereas the traditional style option style expects a block of options and suitable arguments to the options in the right order.

4

It's clearly described in man tar:

Option styles

   Options to GNU tar can be given in three different styles.  In
   traditional style, the first argument is a cluster of option
   letters and all subsequent arguments supply arguments to those
   options that require them.  The arguments are read in the same
   order as the option letters.  Any command line words that
   remain after all options has been processed are treated as
   non-optional arguments: file or archive member names.

   For example, the c option requires creating the archive, the v
   option requests the verbose operation, and the f option takes
   an argument that sets the name of the archive to operate upon.
   The following command, written in the traditional style,
   instructs tar to store all files from the directory /etc into
   the archive file etc.tar verbosely listing the files being
   archived:

   tar cfv a.tar /etc

So in this case:

tar cfv collection.tar blues folk jazz

collection.tar is an argument for f and blues folk jazz being the rest of arguments are treated as archive member names.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .