Saturday 1 February 2020

linux - Why can't -z be the last command-line option to be used with tar?


$ ls one.tar.gz
one.tar.gz
$ tar -xvfz one.tar.gz
tar: z: Cannot open: No such file or directory
tar: Error is not recoverable: exiting now
$ tar -xvzf one.tar.gz
one
$ tar -xzvf one.tar.gz
one
$ tar -zxvf one.tar.gz
one
$

Answer



The -f switch takes an argument (the filename). If z appears after the f then it is taken to be the filename.


tar -xvfz one.tar.gz

is the same as


tar -xvf z one.tar.gz

If you had done this:


tar -xvf -z one.tar.gz

then -z would have been taken as the filename and you'd have gotten a similar error.


This, however, would have worked:


tar -xvz -f one.tar.gz

The GNU tar man page states:



The first argument to tar must be one of the options: Acdrtux, followed by any optional functions. The final arguments to tar are the names of the files or directories which should be archived.



Note that unless input is from stdin or output is to stdout (where appropriate), the the -f filename option and argument must be given. While the man page implies that the order is fixed, in reality the options can be in any order. Even this weird one works (but it may not work in all versions of tar):


tar -v files_to_archive* -f xyz.tar.gz -cz

For portability, it's probably better to stick to the idiomatic argument order and even leave off the hyphen:


tar czvf xyz.tar.gz files_to_archive*

No comments:

Post a Comment

How can I VLOOKUP in multiple Excel documents?

I am trying to VLOOKUP reference data with around 400 seperate Excel files. Is it possible to do this in a quick way rather than doing it m...