I have used my Raspberry Pi for a specific project, where I had to change some configuration in the Rpi, install some dependencies and create a service so it launches at boot.

Now that it's all done, I would like to make a file with my installation as it is so I can copy it into a different SD card for another Raspberry Pi, but just plain using dd gives a file way too big.

I know the official Raspberry Pi images have some configuration set so that they grow the size of the rootfs partition on the first boot to fill the SD card, how do I extract an image of my current installation that does the same thing?

  • Simplest way would be to use gparted to shrink the partition first or use zip to compress the image.
    – user115418
    Sep 8, 2020 at 22:00

2 Answers 2


There are many ways of making an installable image. I use the following script to backup my Pi and this creates images which can be installed on another Pi.

The size of the image created can be set to be as small as possible, although for backup purposes I make this larger.

This does not automatically resize, although it is possible to include this. The original for this script now includes this capability, but it keeps changing and now includes a lot of other "features" of rather specialised use. You could try to include these, but the original is quite complex and undocumented. My version is modified to include backup of files omitted in the original and a few changes to facilitate use on any Pi.


I would recommend several steps:

  1. Eliminate (delete) any extraneous fluff (files, packages, etc...)
  2. "sudo apt-get clean" to get rid of the .deb archives.
  3. Zero out your filespace. e.g. "cp /dev/zero /zerofile ; rm zerofile"

This will rid you of all the left-over bits in the filesystem, leading to a much better compression.

  1. Archive the SD card and compress the image. e.g. " sudo dd if=/dev/path-to-the-source-SD-card bs=512 conv=noerror,sync | gzip -c > /path/to-the-destination-image-file.gz "

To rebuild/create/restore the image: (as root)

" zcat /path/of-the-image-file.gz > /dev/path-to-the-new-card "

Since you are doing this with root privileges, be careful of what device id's you use... You could wipe out your system if not careful...

Also note that you should use the same SD card manufacturer and type as otherwise the perceived geometry may be different.

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.