How can I make tar read input from stdin?

17,291

Using -O with c is ignored, since no files are extracted. You'll get the same result either way:

$ tar c -O foo | tar t
foo
$ tar c foo | tar t  
foo

Which is why I find your error surprising, since tar is no coward if you specify a path.

tar cannot read in file data from stdin for creating an archive, since then tar will have no way of knowing what a file is - where one begins or ends, what its path and metadata is.

Share:
17,291

Related videos on Youtube

kos
Author by

kos

Full-time Linux user. Currently dual-booting Parabola and Ubuntu. I've read and signed the Ubuntu Code of Conduct. My Launchpad profile

Updated on September 18, 2022

Comments

  • kos
    kos over 1 year

    I need to use tar in a pipeline inside a shell script to archive and compress some files.

    After having read the manpage for tar I was able to make tar output to stdout by running it along with the -O argument, but I wasn't able to find out how to make it input from stdin. Since many commands read from stdin if no other input it's specified, I tried:

    pv ~/foo | tar c -O ~/foo.tar
    

    but that didn't work:

    tar: Cowardly refusing to create an empty archive
    
    Try 'tar --help' or 'tar --usage' for more information.
    

    How can I make tar read input from stdin?

    • TobyLL
      TobyLL about 9 years
    • zhongfu
      zhongfu about 9 years
      If you want to display progress, you might want to do the reverse: tar c ~/foo | pv > foo.tar
    • kos
      kos about 9 years
      @zhongfu Thanks, but pv was there because I wanted to see the progress while piping dd output in tar, as you can deduce from my later follow-up question
    • zhongfu
      zhongfu about 9 years
      I didn't really get the point of archiving a disk image instead of storing it as is as it's quite pointless, but what I wanted to say was basically covered by @DavidFoerster's answer at that question, so yeah
    • kos
      kos about 9 years
      @zhongfu I agree with you that in most cases that's plain unuseful, but in that case I wanted to backup also single folders in the same backup file