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


4 Answers 4


Already answered here dd-on-entire-disk-but-do-not-want-empty-portion

Assuming you want to save /dev/sdXN to /tgtfs/image.raw and 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).


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
# 2018-11-29    optional name
# 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"

if [ $# -eq 0 ] ; then

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/$BACKUPNAME`date +%Y%m%d`.img.gz
  • I am very sorry for the late reply I am on Windows10 Commented Sep 3, 2017 at 10:07
  • doesn't work. bs=4m makes dd choke and changing it to 4M also doesn't help Commented Oct 19, 2019 at 8:59
  • @WolfgangFahl "changing it to 4M also doesn't help" hardly surprising because dd on BSD Unix doesn't support this.
    – Milliways
    Commented Oct 19, 2019 at 10:26
  • Since my computer is a MacOS computer i had assumed things would work. Commented Oct 19, 2019 at 10:58
  • @WolfgangFahl: Thanks for your comment as I was considering using this, but it seems to have issues? Did you ever get it to work? If so, would you post your approach as another answer?
    – Seamus
    Commented Jan 27, 2020 at 18:08

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 agree to our terms of service and acknowledge you have read our privacy policy.

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