I was using the answers from Copy files without losing file/folder permissions question, when I ran across this interesting and unexpected behavior.
Using tar cvpfz target.tar.gz dir1
works perfectly, and it is basically the structure of the commands listed in the suggested answers to the question linked above.
Strangely, however, when I added the optional -
before the parameters, (i.e. tar -cvpfz ...
, thinking it was just a stylistic change), that's where the strangeness ensues.
$ tar -cvpfz target.tar.gz dir1
tar: target.tar.gz: Cannot stat: No such file or directory
...
tar: Exiting with failure status due to previous errors
Even stranger, although tar -cvpfz ...
throws the aforementioned error, the command actually works, but instead of creating a gzipped file, it creates a tar file with the name z
(even though it still throws the error).
If I do a tar -tvf z
, it lists the contents of the tar correctly. If I do a tar -ztvf z
, it says that it is not gzipped.
I finally found that changing the command to tar -zcvpf
works perfectly. Somehow, putting the z
parameter first works correctly, however putting it last (i.e. tar -cvpfz ...
) does not!
man tar
seems to indicate that the -
is optional, and one would think the tar command should work identically with or without the -
.
Hopefully this will help someone else who, like me, added the -
, thinking it was just a stylistic difference that wouldn't change the command.
Since I am curious, and would love to learn more about this, can anyone shed light on why this happens?
f
parameter I used, which expects, according to the answers in your link, a file name right after thef
. What comes right after thef
parameter in my example?z
of course, which explains why it created a tar file namedz
rather than treating thez
as a parameter of the tar command. – vocadanz Sep 27 '15 at 07:46z
parameter to the first parameter position (i.e.tar -zcvpf
), it worked perfectly. I guess to be safe, one should always put thef
parameter last (right before the filename), since thef
parameter is expecting the filename to immediately follow it. Thanks for the quick and thorough answer, Muru. Can anyone confirm that my guess is correct as to what is going on? Thanks in advance. – vocadanz Sep 27 '15 at 07:46