4

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?

  • See: unix.stackexchange.com/questions/231265/… – muru Sep 27 '15 at 7:18
  • Brilliant--thanks for that, Muru! From what I understand, then, the problem perhaps lies with the f parameter I used, which expects, according to the answers in your link, a file name right after the f. What comes right after the f parameter in my example? z of course, which explains why it created a tar file named z rather than treating the z as a parameter of the tar command. – vocadanz Sep 27 '15 at 7:46
  • This also explains why, when I moved the z parameter to the first parameter position (i.e. tar -zcvpf), it worked perfectly. I guess to be safe, one should always put the f parameter last (right before the filename), since the f 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 7:46
  • The last answer should make things clearer. – muru Sep 27 '15 at 7:53
  • Absolutely, thank you again, muru. Your link is exactly the answer to the question. Not that you need the points :), but your comment certainly should be (and is) the correct answer! Many thanks! – vocadanz Sep 27 '15 at 7:57
3

The -f option should be followed by a file or device name. When using tar cvpfz target.tar.gz dir1 you are actually using the so-called "Old Option Style" where

When options that need arguments are given together with the command, all the associated arguments follow, in the same order as the options. (tar info page)

  • Thanks for clarifying, and for augmenting with additional details about the "Old Option Style". Good to know that although I'm new to Linux, I'm learning to follow the 'Old' style :). – vocadanz Sep 27 '15 at 9:17
  • Note that 'old option style' refers to "prior to Seventh Edition UNIX", circa 1979. That is, it was non-standard notation already, but retained for backwards compatibility. – Jonathan Leffler Sep 27 '15 at 15:21
1

Muru really answered the question with the links he posted, but due to his encouragement, I will re-copy one of his links and recap my comments here to hopefully make it easier to see the answer.

According to this StackExchange link, the f parameter for the tar command is somewhat special, as it requires an argument (an archive filename) that immediately follows the f parameter, if you are preceding your parameters with a - (e.g. tar -cf). However, as the answer in the link mentions, it may be safer to just forgo the -, so the f parameter doesn't risk treating any parameters you may put after the f as a filename.

This was the problem I ran into as stated in my question, since I tried to put the z parameter right after the f (e.g. tar -cvpfz) which caused the tar command to greedily 'consume' the z and treat it as the archive filename argument to the f parameter, rather than treating the z as simply the tar command's gzip parameter.

In brief, by omitting the - (e.g. using tar cvpfz ... rather than tar -cvpfz ..) the problem that I encountered can be avoided. Now I know why. Thanks, muru!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.