How to decompress only a portion of a file

gzip

I have a large, repetitive text file. It compresses very well – it's about 3MB compressed. However, if decompressed, it takes 1.7GB. Since it's repetitive, I only need a fraction of the output to check the contents of the file.

It was compressed using gzip. Does gunzip provides any way to only decompress the first few megs of a file?

Best Answer

You could decompress to standard output and feed it through something like head to only capture a bit of it:

gunzip -c file.gz | head -c 20M >file.part

The -c flag to head requires the head implementation that is provided by GNU coreutils.

dd may also be used:

gunzip -c file.gz | dd of=file.part bs=1M count=20

Both of these pipelines will copy the first 20 MiB of the uncompressed file to file.part.

Related Question