46

I'm trying to create tar.gz file using the following command:

sudo tar -vcfz dvr_rdk_v1.tar.gz dvr_rdk/

It then start to create files (many files in folder), but then I get the following error:

tar: dvr_rdk_v1.tar.gz: Cannot stat: No such file or directory
tar: Exiting with failure status due to previous errors

I don't see any description of this error, what does it mean?

54

Remove - from vcfz options. tar does not need hyphen for options.

With a hyphen, the argument for the -f option is z. So the command is in effect trying to archive dvr_rdk_v1.tar.gz and dvr_rdk into an archive called z. Without the hyphen, the semantics of the options changes, so that the next argument on the command line, i.e. your archive's filename, becomes the argument to the f flag.

Also check your write permission to the directory from which you are executing the command.

  • 6
    It doesn't need hyphens, but they shouldn't be a problem, they're just optional – Michael Mrozek Aug 10 '14 at 19:00
  • 17
    @MichaelMrozek Actually, removing the - does make a difference, because GNU tar is quirky. Option arguments can't be bundled in the first argument when it doesn't start with a -, so the argument to f is the next argument to tar, and so tar vcfz dvr_rdk_v1.tar.gz is equivalent to tar -v -c -f dvr_rdk_v1.tar.gz -z. – Gilles Aug 10 '14 at 22:19
  • 1
    I also realized if I remove the z or j the archive compression is successful even with - involved. When I add the hyphen is included it treats the last character in the option as a "file". So as a result I get j or z as an archived file if I use this option -cvfz or -cvfj. – JohnnyQ Sep 14 '16 at 2:11
  • 9
    Summary: if 'f' occurs as an option anywhere other as than the last option, then the command will not do what was intended. I don't see how Gnu couldn't add a warning when they detect such a serious and easily-detectable issue. I just spent 40min trying to debug this weirdness. This one must have wasted hundreds of thousands of man-hours of productivity. – smci Oct 25 '17 at 1:14
55

The -f option should directly precede the filename. So, use tar -vczf filename.tar.gz instead of -vcfz

  • Ah this explains why the command I'm using tries to create a compressed archive file called j or z with these commands -cvfj and -cvfz respectively. – JohnnyQ Sep 14 '16 at 2:14
16

The tar command historically has been one of the few commands that doesn't follow the Unix utility syntax guidelines.

The standards page for tar says:

f
Use the first file operand (or the second, if b has already been specified) as the name of the archive instead of the system-dependent default

While the syntax guidelines include this:

Guideline 5:
One or more options without option-arguments, followed by at most one option that takes an option-argument, should be accepted when grouped behind one '-' delimiter.

So while the command you typed, tar -vcfz dvr_rdk_v1.tar.gz dvr_rdk/, would be fine on older versions of tar, certain versions of tar that are written to strictly follow the utility syntax guidelines will parse this to mean "use z as the file argument to -f". So you should use the following to be portable:

tar -cvzf dvr_rdk_v1.tar.gz dvr_rdk/
-1

To create the archive

$ tar -cvf music.tar Music/

This shows the list of file & folders

$ tar -tvf music.tar         

And to extract again

$ tar -xvf music.tar         
  • This does not really explain his error, right? This is just explaining the usage of tar. – Bernhard Aug 10 '14 at 8:23
  • 1
    @Bernhard Well, his error was that he was using tar wrong, although it's nice to at least mention what the key difference is (moving -f to the end) – Michael Mrozek Aug 10 '14 at 19:01
  • 2
    @MichaelMrozek Well, he rather left out the z than put f in the end, hence my comment. – Bernhard Aug 10 '14 at 19:06
  • This command working for ubuntu, It will success – dhamu Sep 6 '14 at 13:23
  • OK! sorry, I am not listening you question, but the command working for ubuntu. – dhamu Sep 6 '14 at 13:32

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.