[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

3.3 The Three Option Styles

There are three styles for writing operations and options to the command line invoking tar. The different styles were developed at different times during the history of tar. These styles will be presented below, from the most recent to the oldest.

Some options must take an argument(2). Where you place the arguments generally depends on which style of options you choose. We will detail specific information relevant to each option style in the sections on the different option styles, below. The differences are subtle, yet can often be very important; incorrect option placement can cause you to overwrite a number of important files. We urge you to note these differences, and only use the option style(s) which makes the most sense to you until you feel comfortable with the others.

Some options may take an argument. Such options may have at most long and short forms, they do not have old style equivalent. The rules for specifying an argument for such options are stricter than those for specifying mandatory arguments. Please, pay special attention to them.


[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

3.3.1 Long Option Style

Each option has at least one long (or mnemonic) name starting with two dashes in a row, e.g., ‘--list’. The long names are more clear than their corresponding short or old names. It sometimes happens that a single long option has many different names which are synonymous, such as ‘--compare’ and ‘--diff’. In addition, long option names can be given unique abbreviations. For example, ‘--cre’ can be used in place of ‘--create’ because there is no other long option which begins with ‘cre’. (One way to find this out is by trying it and seeing what happens; if a particular abbreviation could represent more than one option, tar will tell you that that abbreviation is ambiguous and you’ll know that that abbreviation won’t work. You may also choose to run ‘tar --help’ to see a list of options. Be aware that if you run tar with a unique abbreviation for the long name of an option you didn’t want to use, you are stuck; tar will perform the command as ordered.)

Long options are meant to be obvious and easy to remember, and their meanings are generally easier to discern than those of their corresponding short options (see below). For example:

$ tar --create --verbose --blocking-factor=20 --file=/dev/rmt0

gives a fairly good set of hints about what the command does, even for those not fully acquainted with tar.

Long options which require arguments take those arguments immediately following the option name. There are two ways of specifying a mandatory argument. It can be separated from the option name either by an equal sign, or by any amount of white space characters. For example, the ‘--file’ option (which tells the name of the tar archive) is given a file such as ‘archive.tar’ as argument by using any of the following notations: ‘--file=archive.tar’ or ‘--file archive.tar’.

In contrast, optional arguments must always be introduced using an equal sign. For example, the ‘--backup’ option takes an optional argument specifying backup type. It must be used as ‘--backup=backup-type’.


[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

3.3.2 Short Option Style

Most options also have a short option name. Short options start with a single dash, and are followed by a single character, e.g., ‘-t’ (which is equivalent to ‘--list’). The forms are absolutely identical in function; they are interchangeable.

The short option names are faster to type than long option names.

Short options which require arguments take their arguments immediately following the option, usually separated by white space. It is also possible to stick the argument right after the short option name, using no intervening space. For example, you might write ‘-f archive.tar’ or ‘-farchive.tar’ instead of using ‘--file=archive.tar’. Both ‘--file=archive-name’ and ‘-f archive-name’ denote the option which indicates a specific archive, here named ‘archive.tar’.

Short options which take optional arguments take their arguments immediately following the option letter, without any intervening white space characters.

Short options’ letters may be clumped together, but you are not required to do this (as compared to old options; see below). When short options are clumped as a set, use one (single) dash for them all, e.g., ‘tar -cvf’. Only the last option in such a set is allowed to have an argument(3).

When the options are separated, the argument for each option which requires an argument directly follows that option, as is usual for Unix programs. For example:

$ tar -c -v -b 20 -f /dev/rmt0

If you reorder short options’ locations, be sure to move any arguments that belong to them. If you do not move the arguments properly, you may end up overwriting files.


[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

3.3.3 Old Option Style

As far as we know, all tar programs, GNU and non-GNU, support old options: that is, if the first argument does not start with ‘-’, it is assumed to specify option letters. GNU tar supports old options not only for historical reasons, but also because many people are used to them. If the first argument does not start with a dash, you are announcing the old option style instead of the short option style; old options are decoded differently.

Like short options, old options are single letters. However, old options must be written together as a single clumped set, without spaces separating them or dashes preceding them. This set of letters must be the first to appear on the command line, after the tar program name and some white space; old options cannot appear anywhere else. The letter of an old option is exactly the same letter as the corresponding short option. For example, the old option ‘t’ is the same as the short option ‘-t’, and consequently, the same as the long option ‘--list’. So for example, the command ‘tar cv’ specifies the option ‘-v’ in addition to the operation ‘-c’.

When options that need arguments are given together with the command, all the associated arguments follow, in the same order as the options. Thus, the example given previously could also be written in the old style as follows:

$ tar cvbf 20 /dev/rmt0

Here, ‘20’ is the argument of ‘-b’ and ‘/dev/rmt0’ is the argument of ‘-f’.

The old style syntax can make it difficult to match option letters with their corresponding arguments, and is often confusing. In the command ‘tar cvbf 20 /dev/rmt0’, for example, ‘20’ is the argument for ‘-b’, ‘/dev/rmt0’ is the argument for ‘-f’, and ‘-v’ does not have a corresponding argument. Even using short options like in ‘tar -c -v -b 20 -f /dev/rmt0’ is clearer, putting all arguments next to the option they pertain to.

If you want to reorder the letters in the old option argument, be sure to reorder any corresponding argument appropriately.

This old way of writing tar options can surprise even experienced users. For example, the two commands:

tar cfz archive.tar.gz file
tar -cfz archive.tar.gz file

are quite different. The first example uses ‘archive.tar.gz’ as the value for option ‘f’ and recognizes the option ‘z’. The second example, however, uses ‘z’ as the value for option ‘f’ — probably not what was intended.

This second example could be corrected in many ways, among which the following are equivalent:

tar -czf archive.tar.gz file
tar -cf archive.tar.gz -z file
tar cf archive.tar.gz -z file

[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

3.3.4 Mixing Option Styles

All three styles may be intermixed in a single tar command, so long as the rules for each style are fully respected(4). Old style options and either of the modern styles of options may be mixed within a single tar command. However, old style options must be introduced as the first arguments only, following the rule for old options (old options must appear directly after the tar command and some white space). Modern options may be given only after all arguments to the old options have been collected. If this rule is not respected, a modern option might be falsely interpreted as the value of the argument to one of the old style options.

For example, all the following commands are wholly equivalent, and illustrate the many combinations and orderings of option styles.

tar --create --file=archive.tar
tar --create -f archive.tar
tar --create -farchive.tar
tar --file=archive.tar --create
tar --file=archive.tar -c
tar -c --file=archive.tar
tar -c -f archive.tar
tar -c -farchive.tar
tar -cf archive.tar
tar -cfarchive.tar
tar -f archive.tar --create
tar -f archive.tar -c
tar -farchive.tar --create
tar -farchive.tar -c
tar c --file=archive.tar
tar c -f archive.tar
tar c -farchive.tar
tar cf archive.tar
tar f archive.tar --create
tar f archive.tar -c
tar fc archive.tar

On the other hand, the following commands are not equivalent to the previous set:

tar -f -c archive.tar
tar -fc archive.tar
tar -fcarchive.tar
tar -farchive.tarc
tar cfarchive.tar

These last examples mean something completely different from what the user intended (judging based on the example in the previous set which uses long options, whose intent is therefore very clear). The first four specify that the tar archive would be a file named ‘-c’, ‘c’, ‘carchive.tar’ or ‘archive.tarc’, respectively. The first two examples also specify a single non-option, name argument having the value ‘archive.tar’. The last example contains only old style option letters (repeating option ‘c’ twice), not all of which are meaningful (eg., ‘.’, ‘h’, or ‘i’), with no argument value.


[ << ] [ < ] [ Up ] [ > ] [ >> ]

This document was generated on August 23, 2023 using texi2html 5.0.