I did a backup for my RPi (64GB microsd card) and it saved the entire 64GB worth .img file... while theres only ~14GB worth actual data, the image file accounts also for that empty space.

Anyone know if theres a way to remove that empty space from the .img file?

Many thanks in advance :-) EDIT: I am using Windows10

https://www.raspberrypi.org/documentation/linux/filesystem/backup.md explains backup and restoration.

You don't indicate what OS you are using but it is simple on any 'NIX system. I use the following on macOS

# script to backup Pi SD card
# 2017-06-05
# DSK='disk4'   # manual set disk
# Find disk with Linux partition (works for Raspbian)
# Modified for PINN/NOOBS
export DSK=`diskutil list | grep "Linux" | sed 's/.*\(disk[0-9]\).*/\1/' | uniq`
if [ $DSK ]; then
    echo $DSK
    echo $OUTDIR
    echo "Disk not found"

diskutil unmountDisk /dev/$DSK
echo please wait - This takes some time
echo Ctl+T to show progress!
time sudo dd if=/dev/r$DSK bs=4m | gzip -9 > $OUTDIR/Piback.img.gz

#rename to current date
echo compressing completed - now renaming
mv -n $OUTDIR/Piback.img.gz $OUTDIR/Piback`date +%Y%m%d`.img.gz
  • I am very sorry for the late reply I am on Windows10 – Mohd Salman Sep 3 '17 at 10:07

Already answered here https://serverfault.com/questions/439128/dd-on-entire-disk-but-do-not-want-empty-portion

Assuming you want to save /dev/sdXN to /tgtfs/image.rawand you are root:

mkdir /srcfs && mount /dev/sdXN /srcfs

Use zerofill or just:

dd if=/dev/zero of=/srcfs/tmpzero.txt

To fill unused blocks with zero (wait for it to fill the file system completely then

rm /srcfs/tmpzero.txt)

Take the image with dd and use conv=sparse to punch zeros on-the-fly:

dd conv=sparse if=/dev/sdxn of=/tgtfs/image.raw

If you want to use compression you don't need to punch the zeros with dd as zero blocks are highly compressible:

dd if=/dev/sdxn | gz -c | dd of=/tgtfs/image.raw

PS: You should note that this is not a good idea on a flash memory based storage media (i.e. your source file system be SSD).

Shrink your second partition with gparted to eliminate free space and use dd to only backup the parts you need.

I suggest using a gparted Live CD (based on Linux) to modify RPi partitions. You can boot from such a disk without touching your Windows install at all. This is much safer.

gparted Screenshot

You can now use dd to only backup the parts of the SD card that matter.

Any sensible compression program will automatically detect free space and collapse it efficiently.

Either use raspiBackup to create the dd image and read FAQ 16 or use pishrink.

Your Answer


By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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