I was using the answers from Copy files without losing file/folder permissions question, when I ran across this interesting and unexpected behavior.
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?