93

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?

Kusalananda
  • 333,661
ransh
  • 1,407

5 Answers5

101

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.

Kusalananda
  • 333,661
Velnix
  • 1,142
  • 7
    It doesn't need hyphens, but they shouldn't be a problem, they're just optional – Michael Mrozek Aug 10 '14 at 19:00
  • 25
    @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 'SO- stop being evil' 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 02:11
  • 23
    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 01:14
  • I had to remove the hyphen to make it work, although I have used the hyphen many times on other systems. Can't they just keep the syntax consistent??! – Stonecraft Jan 31 '20 at 03:04
  • tar is my least favorite cli tool. Why is it the way it is? – Josh M. Apr 01 '23 at 01:28
86

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

Bernhard
  • 12,272
twan163
  • 5,690
19

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/
Mark Plotnick
  • 25,413
  • 3
  • 64
  • 82
1

Notice that the -f needs to go immediately before the archive name but after the --exclude options in this Makefile multi-line example...

-f $(HOME)/.bitcoin-$(TIME).tar.gz $(HOME)/.bitcoin'  

#######################
#Backup $HOME/.bitcoin - Makefile usage example
########################


TIME=$(shell date +%s)  
export TIME  
init:  
    @echo ''  
    bash -c 'mkdir -p $(HOME)/.bitcoin'  
    bash -c 'conf/get_size.sh'  
    bash -c 'tar czv --exclude=*.log --exclude=banlist.dat \  
            --exclude=fee_exstimates.dat --exclude=mempool.dat \  
            --exclude=peers.dat --exclude=.cookie \  
            --exclude=.lock --exclude=.walletlock \  
            -f $(HOME)/.bitcoin-$(TIME).tar.gz $(HOME)/.bitcoin'  
    #install current bitcoin.conf  
    bash -c 'install -v conf/bitcoin.conf $(HOME)/.bitcoin'  
    @echo ''  
Hauke Laging
  • 90,279
0

For future readers, this post was the answer for me. Keep wildcards outside the quotes: tar -zcf archive.tar.gz "file-0"*

bfuzze
  • 101