2

I used gzip to compress an image which is quite huge still.

dd if=/dev/sda2 bs=1M | gzip -c -9 > sda2.dd.img.gz 

then I changed the partitioning of the Drive because I wanted to install Linux. And when trying to decompress and write it to the former (smaller) partition

gunzip sda2.dd.img.gz >/dev/sda2 

(using > instead of dd, as described here, its content is first written to another file (instead of directly to the partition as I expected).

The Problem: the Partition where the zipped file is on is to. small. I could use a external media but that will take more time. But would prefer to write the decompressed data to the partition directly.

So I decided to get more info about zipping itself first. but it left me even more confused (see my first response here) Can anyone give any hint, please ?

thanks in advance,

2
  • 1
    It looks as if you’re restoring the data obtained from a partition to the disk rather than to a partition, or is that a typo in the text? Commented Feb 24 at 18:46
  • According to the man page, gunziponly works on files: it replaces each named file with its uncompressed version. If you want to write to stdout, use zcat. Commented Feb 24 at 23:03

2 Answers 2

2

This

gunzip filename.gz 

will write to filename.

This

gunzip filename.gz > output 

will still write to filename (and produce an empty output file).

You probably meant to use

gunzip < filename.gz > output 

which will read from stdin and write to stdout, so the redirections work as expected.

1
  • thanks a lot for your answer, which worked for me today. and helped solving the problem your answer was very educational and helping! Commented Feb 27 at 14:06
1

(using > instead of dd, as described here, its content is first written to another file (instead of directly to the partition as I expected).

No, that's not correct. > /dev/sdh makes the kernel directly write the decompressed data to /dev/sdh. However, gunzip file.gz decompresses to file, not standard output. As Frostschutz correctly points out (and as I missed), you want gunzip < file.gz > destination.

So, if it doesn't fit afterwards, the original partition /dev/sda<n> was simply larger than /dev/sdh. This is completely independent from any compression used!


Remarks:

  • you're reading from a partition, but you're writing to a complete device. That usually is not right. So, make sure you're trying to achieve the right thing!
  • your dd on the input is also not very helpful; you'd probably be getting better performance if you just did < /dev/sda<n> instead of dd if=/dev/sda<n> bs=1M |, i.e.
    n=… gzip -c -9 < /dev/sda${n} > sda${n}.dd.img.gz 
  • gzip -9 is relatively slow (pigz is faster and does exactly the same!), but still pretty bad at compression. Maybe try
    # TO COMPRESS: n=… zstd -f -T0 -10 /dev/sda${n} > sda${n}.img.zstd # -f: allow reading from a block device directly # -T0: use all available CPU cores for compression # -10: compression "effort" 10. Should be still faster than `gzip -9`. # Goes up to 18, if you want to experiment. # TO UNPACK: zstd -f -d sda${n}.img.zstd > /dev/sdh # -f: allow direct writing to a block device # -d: decompress 
    instead.
6
  • "So, if it doesn't fit afterwards, the original partition /dev/sda<n> was simply larger than /dev/sdh. This is completely independent from any compression used!" Yes, that's correct. I first made a backup (of Windows) then reduced the size (with cfdisk) to install linux. And then after, the Windows didn't run any more so I wanted to reinstall it on the now smaller partition! About the mistake you are pointing out: I was not quite correct. The command I used was: gunzip sda<n>.dd.img.gz >/dev/sda<n> Commented Feb 24 at 21:54
  • So you mean I have to resize the Partition to it's original size, reinstall Windows, defrag it, resize the the Partition and make the backup again?.. ?? UH! Commented Feb 24 at 22:01
  • 1
    well, what did you expect when you made a byte-exact backup of something larger than what you'll restore it to? Commented Feb 24 at 22:50
  • As frostschutz correctly says, gunzip file.gz writes the decompressed data to 'file' not to stdout, so it cannot be redirected (or piped). gunzip <file.gz does write to stdout, as does gunzip -c file.gz, and as Paul_Pedant commented zcat file.gz (zcat is a script that just does gzip -dc while gunzip just does gzip -d). Commented Feb 25 at 6:09
  • @dave_thompson_085 aaargh, missed that. Fixed my answer. Commented Feb 25 at 11:46

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.