32

I am under the following restrictions:

  • I have a 1.0 GB .zip file on my computer which contains one file, a disk image of raspbian. When uncompressed, this file is 3.2 GB large and named 2015-11-21-raspbian-jessie.img.
  • After having downloaded the zip file, I have just under 1.0 GB of storage space on my computer, not enough space to extract the image to my computer.
  • This file needs to be uncompressed and written to an SD card using plain old dd.

Is it possible for me to write the image to the SD card under these restrictions?

I know it's possible to pipe data through tar and then pipe that data elsewhere, however, will this still work for the zip file format, or does the entire archive need to be uncompressed before any files are accessible?

gatorback
  • 1,384
  • 23
  • 48
IQAndreas
  • 10,345
  • I realize I could just extract the zip file to an external flash drive, but this is more fun. I want to pull this off for the challenge (and learning experience) of it! – IQAndreas Aug 29 '15 at 05:14
  • 6
    If your SD card is device /dev/sdh, then run unzip -p file.zip >/dev/sdh. (I know that the use of dd is traditional but it is not necessary.) – John1024 Aug 29 '15 at 05:24
  • Huh. Would that work without dd? I figured the Block Size parameter would be needed. – CRThaze Aug 29 '15 at 06:46
  • 4
    Yes, it works without dd. In the past, block size was very important when writing to tapes. For disks, it doesn't matter. – John1024 Aug 29 '15 at 07:38
  • 1
    @TheCzar Aside from tapes, the block size parameter to dd can only lose data or slow things down (or in rare case speed things up a little, if large enough). Forget about dd, it's very rarely useful. – Gilles 'SO- stop being evil' Aug 29 '15 at 16:25

3 Answers3

39

Use unzip -p:

unzip -p 2015-11-21-raspbian-jessie.zip 2015-11-21-raspbian-jessie.img | dd of=/dev/sdb bs=1M

-p extracts files to stdout

CervEd
  • 174
yaegashi
  • 12,326
  • The code is running, and neither my harddrive space nor my RAM is running out. All good so far. – IQAndreas Aug 29 '15 at 05:35
  • 7
    Or simply unzip -p … >/dev/sdb. – Gilles 'SO- stop being evil' Aug 29 '15 at 16:25
  • 2
    @Gilles besides the downside, dd is sometimes useful with sudo: unzip -p ... | sudo dd of=/dev/sdb bs=1M – yaegashi Aug 29 '15 at 17:17
  • 7
    @yaegashi unzip -p … | sudo 'cat >/dev/sdb' But I prefer sudo chown $USER /dev/sdb which gives me an extra opportunity to check that I'm writing to the right device. – Gilles 'SO- stop being evil' Aug 29 '15 at 17:26
  • It worked beautifully! Mind if I edit in the exact file name in case anyone just wants to copy and paste the command? – IQAndreas Sep 08 '15 at 15:53
  • What if the archive is xz? I'm finding that the archive is first being extracted in full before committing it to disk. If the archive is huge as it is in my case then the whole source disk gets full before the operation can complete. Should I be using another archive format instead of xz? –  Sep 30 '20 at 21:17
  • @Gilles: I get sudo: cat >/dev/sdb: command not found.  I feel like the emperor has no clothes; I believe that your sudo 'cat >/dev/sdb' command would never work, and would never have worked, but here I am, posting the first comment on it, after almost six years.  Am I missing something? – G-Man Says 'Reinstate Monica' May 04 '21 at 18:14
  • 1
    @G-ManSays'ReinstateMonica' No, that should have been sudo -s 'cat >/dev/sdb' – Gilles 'SO- stop being evil' May 04 '21 at 18:17
3

After a bit of struggling with the former solution:

 unzip -p ~/Downloads/2020-02-05-raspbian-buster-lite.zip | sudo dd of=/dev/disk2 bs=1m

or, if you want to see the progress and you have installed pv:

 unzip -p ~/Downloads/2020-02-05-raspbian-buster-lite.zip | pv | sudo dd of=/dev/disk2 bs=1m
0

I typically use unzip -p 2015-11-21-raspbian-jessie.zip >/dev/sdb or zcat 2015-11-21-raspbian-jessie.gz >/dev/sdb for convenience. This is because when doing large data transfer, we expect the operating system to automatically adjust block size (according to source block size, target block size, memory availability, etc.) for optimal performance, and it turns out they work faster than dd if= of= bs= (maybe because I did not by-brute-force try out all possible choices of blocksizes for dd) ^_^