Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a DD image from a 4GB SD card that has two partitions, these two partitions are only using up about 800 MB and as such I wish to reduce the size of the img fie.

Does anyone know of a way to remove the "free space" from the img file?

share|improve this question
you may want to add what the desired outcome is. ie: image back to the card, to store on CD. – Wayne Jun 22 '13 at 16:41
up vote 2 down vote accepted

First make sure the free space is actually empty, and doesn't contain leftovers of deleted files. The easiest way to achieve this is to create a huge file on the disk, containing only null bytes, then delete it.

# losetup --find --partscan foo.img
# lsblk
loop0       7:0    0   4096M  0 loop 
├─loop0p1 259:0    0   2048M  0 loop 
└─loop0p2 259:1    0   2048M  0 loop 
# for part in /dev/loop0p*; do
    mount $part /mnt
    dd if=/dev/zero of=/mnt/filler conv=fsync bs=1M
    rm /mnt/filler
    umount /mnt
dd: error writing ‘/mnt/filler’: No space left on device
dd: error writing ‘/mnt/filler’: No space left on device
# losetup --detach /dev/loop0

Then compress it with a tool like gzip or xz. Even at lowest compression levels, a long series of zeros will compress well:

# ls -s
4096M foo.img
# gzip foo.img
# ls -s
11M foo.img.gz

Note that you must uncompress the image when writing it back to disk. But this can be done without needing any space for the full image:

# cat foo.img.gz | gunzip | dd of=/dev/sda

An alternative method, if you want to keep using the image – e.g. with a virtual machine – is to convert the raw image to one of the image formats used by virtualization software; e.g. qcow2 for Qemu, VDI for VirtualBox, or VMDK for VMware.

Note that this still requires you to prepare the image by cleaning the free space using the above method.

# qemu-img convert -f raw -O qcow2 foo.img foo.qcow

# qemu-img convert -f raw -O vmdk foo.img foo.vmdk

But if it's going to be written to a real disk again, you have to convert it back to a raw image.

share|improve this answer
how can you be sure that all of the files that are currently stored on the partitions aren't physically scattered around on the disk? – Valerio Santinelli Jun 18 '14 at 13:38
@ValerioSantinelli: They usually are, but why should I care about that? – grawity Jun 18 '14 at 15:26
because I suppose that files aren't written sequentially on the disk and if a file is at the end of the storage, when I cut the size down, I'll lose the content of that file. – Valerio Santinelli Jun 18 '14 at 17:35
@ValerioSantinelli: How is this at all related to my post? – grawity Jun 19 '14 at 16:56
Because the OP is asking how to shrink a disk image without losing data. If you truncate an image at a certain size you can't be sure the data will be preserved. – Valerio Santinelli Jun 19 '14 at 17:05

Using resize2fs is much much easier

resize2fs -M xxx.img

you will be asked to e2fsck first - so:

e2fsck -f -y xxx.img

(image must NOT be mounted!)

Note: this will only work if the image is of a single partition, if it's a whole block device with mutiple partitions see above answer...

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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