20

I am learning Linux. I was surprised to see that the parameter order seems to matter when making a tarball.

tar -cfvz casual.tar.gz snapback.txt bucket.txt

gives the error:

tar: casual.tar.gz: Cannot stat: No such file or directory
tar: Exiting with failure status due to previous errors

But if I issue the command like this:

tar -cvzf casual.tar.gz snapback.txt bucket.txt

the tarball is created without errors

Can anyone explain to me why the parameter order matters in this example or where I can find that information to learn why myself? I tried it the way I did in my first example that received an error with the logic of putting the required parameters c and f first followed by my other parameters.

I want to completely absorb Linux, which includes understanding why things like this occur. Thanks in advance!

2
  • 7
    Obligatory XKCD
    – Pavel
    Oct 28, 2015 at 10:52
  • 2
    In general, you can use the parameters of most commands in any order, but if a parameter expects some value, you have to give that value to the right parameter. The f stands for file, and expects you to give it some filename. Assume you want to also give the H parameter, to set the format. Out of tar -c -H pax -f my.pax file, tar -cH my.pax -f pax file, tar -cHf my.pax pax file, tar -cHf pax my.pax file etc. only one is really clear. But if you just switch position like tar -cf my.pax -H pax file it is equaly clear and valid. Just switch position of the arguments too!
    – Josef
    Oct 28, 2015 at 11:57

2 Answers 2

35

Whether the order matters depends on whether you start the options with a minus

$ tar -cfvz casual.tar.gz snapback.txt bucket.txt
tar: casual.tar.gz: Cannot stat: No such file or directory
tar: Exiting with failure status due to previous errors

$ tar cfvz casual.tar.gz snapback.txt bucket.txt
snapback.txt
bucket.txt

This unusual behavior is documented in the man page

 Options to GNU tar can be given in three different styles.
 In traditional style
 ...
 Any command line words that remain after all options has
 been processed are treated as non-optional arguments: file or archive
 member names.
 ...
 tar cfv a.tar /etc
 ...
 In UNIX or short-option style, each option letter is prefixed with a
 single dash, as in other command line utilities.  If an option takes
 argument, the argument follows it, either as a separate command line
 word, or immediately following the option.
 ...
 tar -cvf a.tar /etc
 ...
 In GNU or long-option style, each option begins with two dashes and
 has a meaningful name
 ...
 tar --create --file a.tar --verbose /etc

tar, which is short for "tape archive" has been around before the current conventions were decided on, so it keeps the different modes for compatibility.

So to "absorb Linux", I'd suggest a few starting lessons:

  • always read the man page
  • minor differences in syntax are sometimes important
    • the position of items - most commands require options to be the first thing after the command name
    • whether a minus is required (like tar, ps works differently depending on whether there is a minus at the start)
    • whether a space is optional, required, or must not be there (xargs -ifoo is different from xargs -i foo)
  • some things don't work the way you'd expect

To get the behavior you want in the usual style, put the output file name directly after the f or -f, e.g.

$ tar -cvzf casual.tar.gz snapback.txt bucket.txt
snapback.txt
bucket.txt

or:

$ tar -c -f casual.tar.gz -z -v snapback.txt bucket.txt

or you could use the less common but easier to read GNU long style:

$ tar --create --verbose -gzip --file casual.tar.gz snapback.txt bucket.txt
0
9

Checking the -f option in the tar man page gives us:

 -f, --file ARCHIVE
       use archive file or device ARCHIVE

That means that the next word that follows the -f option is going to be the file's name. In your example, after the -f it follows the letters vz, which are going to be used as the file's name.

After that it parses every argument as its input to compress into the file now called vz. When it tries to do so, tar notices that there is no file with the name casual.tar.gz to compress, and then it gives your error.

Therefore, after the -f, it must always follow the name of the file that you want to produce.

4
  • So I could have used the other parameters in any order, I just need to make sure when I use the f parameter I use it last with my tarball filename directly thereafter. Correct?
    – BitBug
    Oct 28, 2015 at 3:56
  • 2
    @BitBug In the most common calling convention, yes, put the f as the last letter in your options block.
    – Mikel
    Oct 28, 2015 at 3:58
  • 1
    If you use the options with the - prefixed, the f must be the last, the parameters that don't need any argument can be used in any order. Alternatively you could simply not write the -.
    – Kira
    Oct 28, 2015 at 4:00
  • 4
    But note that this possibility to not need the dash prefix is a tar's particularity, most unix tools won't allow that.
    – Kira
    Oct 28, 2015 at 4:02

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