Does parameter order matter with tar?

BitBug

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!

Mikel

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Does HTTP URL parameter order matter for caching?

From Dev

In the ABBYY Cloud OCR SDK, does the order of Language parameter values matter?

From Java

Does annotations order matter?

From Dev

Does order of conditions in $and matter?

From Dev

SQL - does order of OR conditions matter?

From Java

Does the order of members in a struct matter?

From Dev

Does the order of natural joins matter

From Dev

Does the order of subscribeOn and observeOn matter?

From Dev

Does Python import order matter

From Dev

Does variable order matter for sscanf?

From Dev

Does the order of a Java class matter?

From Dev

Prolog Does the order of recursion matter?

From Dev

Prolog Does the order of recursion matter?

From Dev

xargs: Does order of options matter?

From Dev

Does the order of rules in ABNF matter?

From Dev

.zshrc configuration, does order matter?

From Dev

Does the order of header properties matter?

From Dev

Does the order of constraints in a pred matter?

From Dev

Does order of html meta tags matter?

From Dev

Does the order of partitioned columns in WHERE clause matter

From Dev

Does the order of Babel 6 presets matter?

From Dev

Does class/function order matter in C++?

From Dev

Why does the order of css selectors matter?

From Dev

Boolean equal: 0 == a, does operand order matter?

From Dev

Does order of inheritance between class and interface matter?

From Dev

Does the order in which dynamic libraries are loaded matter?

From Java

Why does order of mutable borrows matter in Rust?

From Dev

Why does declaration order matter for generic members?

From Dev

Does the order of implicit parameters matter in Scala?

Related Related

HotTag

Archive