Click to See Complete Forum and Search --> : piping tar to gzip?


soleblazer
09-28-2001, 12:34 PM
I know I can use gnu tar to do the following using the z option, but can anyone tell me why this doesn't work?

tar -cvf - filename | gzip -

I always get this error:
"a AppNote.ps 318K
gzip: compressed data not written to a terminal. Use -f to force compression."

I dont feel like putting gtar on my mix of Linux/Sun (Sun I guess) and I can go from gunzip to tar...

Tks

jc

[ 28 September 2001: Message edited by: soleblazer ]

Alex Cavnar, aka alc6379
09-28-2001, 12:50 PM
I think its because you used the -f option. If you do it without that, tar outputs to STDOUT instead of a filename. I'm not sure how to get gzip to handle filenames, though.

xh3g
09-28-2001, 09:59 PM
i know there's got to be a cleaner way but this works.
tar cvf filename.tar filename | gzip filename.tar

Craig McPherson
09-28-2001, 11:16 PM
tar cvf filename.tar filename | gzip filename.tar

Umm... that's not right. That's seriously messed up. That tar command won't dump anything to stdout other than the list of files it compressed, which gzip will ignore since you've specified a file for it to zip. In short, those are two completely seperate commands: the pipe use useless. You might have well have just used a ; or && instead of a useless pipe.

What you're looking for is this:

tar cv files-to-compress | gzip > archive.tar.gz

Peep this -- "tar c" by default tars a bunch of files/directories specified on the command line (there's no need to use tar with just one file or with just a stream of text), and writes them to stdout, unless you use the "f" option, which writes the tarred files to a file instead of to stdout.

Gzip, by default, takes its input from stdin, and then writes it to stdout -- if stdout is a terminal, it'll balk because there's no real reason why you'd want gzip to write its output to a terminal. Generally you'll redirect the output to a file. Gzip can also run with a filename as an argument, in which case it does not take data from stdin or output to stdout, rather, it reads in the file and outputs its output to filename.gz.

I know it can be a little complex but it's logical.