Get compressed size of piped output with gzip

compressiongzippipe

To get uncompressed size of already compressed file, I can use -l option in gzip utility:

gzip -l compressedfile.gz

However is there a way to get size of compressed file if I am piping the output? For example using this command:

gzip -fc testfile > testfile.gz

or specifically if I am redirecting output somewhere where I might not have direct access (server)

gzip -fc testfile > /dev/null
gzip -fc testfile | ssh serverip "cat > file.gz"

Can this be done? I need either the compression ratio or the compressed size.

Best Answer

dd to the rescue.

gzip -fc testfile | dd of=testfile.gz
0+1 records in
0+1 records out
42 bytes (42 B) copied, 0.00018711 s, 224 kB/s

or in your ssh example

gzip -fc testfile | dd | ssh serverip "cat > file.gz"
0+1 records in
0+1 records out
42 bytes (42 B) copied, 0.00018711 s, 224 kB/s

And then just parse the output of the commands using awk or somesuch to pluck out the crucial part of the last line.

Related Question