I've got a bootable flash memory card with an Ubuntu operating system on it along with other programs, ie. a fully functioning system I use on a single-board computer. I'd like to back it up on my PC's (also running Ubuntu) hard disk, but without wasting space as the dd command would do (because it also backs up free space). The flash drive is 32GB big and only 10% full. It doesn't have any partitions on it.

What are the options?

EDIT the flash drive is formatted as ext4, so partimage doesn't apply.


If you want to use dd because with it you can cook very quickly other flash memory:

  • set to zero all free space: dd if=/dev/zero of=/media/usb_device_dir/ZEROFILL ; rm /media/usb_device_dir/ZEROFILL this creates a big file filled with zero and then removes it.

  • dd if=/dev/usb_device | bzip2 > os_image.bz2

Or make a tar of your operating system:

cd /media/usb_device_dir
tar jcpvf /home/foo/os_image.tar.bz2 . 

But before extracting this to a clean flash card, you have to

  • format

  • add bootable flag

  • install a bootloader

  • Hi Alessandro. Could you please explain how the first statement creates a "big file"? What is it's size? Should I replace /dev/zero with anything, or is it a special command to dd? – mmm Dec 6 '11 at 16:45
  • And another thing: shouldn't be rm /media/usb_device_dir/*ZEROFILL* ? – mmm Dec 6 '11 at 17:21
  • Yes, you are right, it is /media/usb_device_dir/ZEROFILL just a copy-paste error. It writes zero to that file until there's no space available. – Alessandro Pezzato Dec 6 '11 at 17:57
  • Just a note / adding parameter bs=10M to the dd command speeds up the progress dramatically on my PC – Kamil Šrot Aug 8 '14 at 15:24

dd if=/dev/your_usb | lzma -9c > usb_image.bin.lzma shouldn't waste any space.

  • Thanks Let_Me_Be, is there a way to make this particular dd command execute a bit faster? Eg. less compression? Is there a way for the dd command to output any progress indication? And last but not least, what is the command to uncompress the image to your_usb afterwards? – mmm Dec 6 '11 at 16:47
  • 1
    @mmm lzcat usb_image.bin.lzma | dd of=/dev/your_usb – Šimon Tóth Dec 6 '11 at 17:10
  • pv /dev/your_usb | lzma -9c > usb_image.bin.lzma wil show progress (requires package pv installed). – Jaap Eldering Dec 6 '11 at 17:39

If you can assume that empty areas on the filesystem are filled with zeros Let_Me_Be's answer applies.

Otherwise have a look at Partimage

  • Is it likely that ext3/ext4 will fill empty space with zeroes? I have a feeling that it is not so, but have nothing to back it up but performance considerations. – user49531 Dec 6 '11 at 16:02
  • Thanks halo, I edited my question, partimage doesn't support ext4, unfortunately. – mmm Dec 6 '11 at 16:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.