Why can't -z be the last command-line option to be used with tar?
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*
z can be used last:
tar -x -v -f foo.tar.gz -z
But as stated by @Dennis, the f flag takes an argument.