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?
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.
Then compress it with a tool like
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:
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.
But if it's going to be written to a real disk again, you have to convert it back to a raw image.
you will be asked to e2fsck first - so:
(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...