Home Raspberry Pi PiShrink – Make Raspberry Pi Images Smaller

PiShrink – Make Raspberry Pi Images Smaller

By sk
Published: Updated: 45.1K views

Raspberry Pi requires no introduction. It is a small, affordable and credit-card sized computer that can be connected to a Monitor or TV. We can attach a standard keyboard and mouse and use it as a full-blown desktop computer to do everyday tasks, such Internet browsing, playing videos/games, word processing and spreadsheet making and a lot more. It has been mainly developed for teaching Computer science in schools. Nowadays, Raspberry Pi is widely being used in colleges, small-medium organizations and institutes to teach coding. If you own a Raspberry Pi device, you might want to check out a bash script named "PiShrink", which is used to make Raspberry Pi Images smaller.

PiShrink will automatically shrink a pi image that will then resize to the max size of the SD card on boot. This will make putting the image back onto the SD card faster and the shrunk images will compress better. This can be useful to fit the large size images in your SD card. In this brief guide, we are going to learn to shrink Raspberry images to smaller size in Linux and Unix-like systems.

Install PiShrink in Linux

To install PiShrink on your Linux box, first download the latest version using command:

$ wget https://raw.githubusercontent.com/Drewsif/PiShrink/master/pishrink.sh

Next, make the downloaded PiShrink binary as executable:

$ chmod +x pishrink.sh

Finally, move it your path:

$ sudo mv pishrink.sh /usr/local/bin/

Shrink Raspberry Pi Images Using PiShrink

As you may already know, Raspbian is the official operating system for all models of Raspberry Pi. The Raspberry foundation has developed Raspberry Pi Desktop version for PC and Mac. You can create a live cd, run it in virtual machine and even install it in your desktop as well. There are also few unofficial OS images available for Raspberry Pi. For the purpose of testing, I've downloaded the official Raspbian OS from the official download page.

Unzip the downloaded OS image:

$ unzip 2019-04-08-raspbian-stretch-lite.zip

The above command will extract contents of 2019-04-08-raspbian-stretch-lite.zip file in the current working directory.

Let check the actual size of the extracted file:

$ du -h 2019-04-08-raspbian-stretch-lite.img 
1.7G 2019-04-08-raspbian-stretch-lite.img

As you can see, the size of the extracted Raspberry OS img file is 1.7G.

Now, shrink this file's size using PiShrink like below:

$ sudo pishrink.sh 2019-04-08-raspbian-stretch-lite.img

Sample output:

Creating new /etc/rc.local
rootfs: 39795/107072 files (0.1% non-contiguous), 239386/428032 blocks
resize2fs 1.45.0 (6-Mar-2019)
resize2fs 1.45.0 (6-Mar-2019)
Resizing the filesystem on /dev/loop1 to 280763 (4k) blocks.
Begin pass 3 (max = 14)
Begin pass 4 (max = 3728)
The filesystem on /dev/loop1 is now 280763 (4k) blocks long.

Shrunk 2019-04-08-raspbian-stretch-lite.img from 1.7G to 1.2G
Make Raspberry Pi Images Smaller Using PiShrink
Make Raspberry Pi Images Smaller Using PiShrink

As you see in the above output, the size of the Rasberry Pi image has been reduced to 1.2G.

You can also use -s flag to skip the autoexpanding part of the process.

$ sudo pishrink.sh -s 2019-04-08-raspbian-stretch-lite.img newpi.img

This will create a copy of source img file (i.e 2019-04-08-raspbian-stretch-lite.img) into a new img file (newpi.img) and work on it. For more details, check the official GitHub page given at the end.


You May Also Like


David Foster October 20, 2019 - 9:14 pm

It TOTALLY AMAZES ME that after all this time there does not exist a simple utility that can create a properly sized Raspbian backup image!

Rob van Breemen February 10, 2020 - 6:11 am

pls remove my ps in my reply
i had forgotten to “sudo mv pishrink.sh /usr/local/bin/” :> so the script is even bether i just heve to learn reading 😉

Bobby Cooper June 9, 2020 - 11:24 am

SK, Thank You So much for your PiShrink script ! Works great, per your instructions !!!

Koh Joo Siang June 9, 2020 - 3:40 pm

Bravo SK, very useful script you made and shared with us. Thank you very much and make my life so much easy.

Patrick July 11, 2020 - 7:23 am

Is there a way (short of dd and then pishrink) to take the SD Card, and create a shrunk image that will expand on boot? I’m assuming that I’ll have to use dd and then pishrink the img file. But, I’m hoping there’s a built-in way to do it.

sk July 11, 2020 - 12:09 pm

I have no idea how to do it exactly. I came across this guide after a quick google search. May be this might help. https://blog.dhampir.no/content/shrinking-a-raspbian-installation-and-re-enabling-auto-expanding-for-distribution-of-customized-images

Also try Raspberry pi stackexchange forum. https://raspberrypi.stackexchange.com/

Rob September 1, 2020 - 7:10 pm

The file system seems to expand automatically – at least in Raspios-Buster. I created an image where I’d done all the updates (which takes forever) Shrunk it down on a Pi from 16Gb to 4Gb then inserted into a Pi4 and booted up. It was slow first time around and at one point seemed to restart. I enabled VNC and had a look and the file system is reporting 10.1GB free out of as total of 13.9Gb.

That’s all I did folks.

B Smith September 13, 2020 - 11:59 pm

Wow – HUGE thanks for this – can’t tell you how many times I have tried but failed to shrink an img – (clearly beyond my skill but a significant need) . Worked fantastic, just remember you need let it resize the file system to the new SD size, and reboot – for me a couple of times to complete. So helpful when you have an image file from one vendor card and try to re-image it to another vendor card that is of course, not the exact same size, and fails because it is too big to fit – great solition! I had a 32gb image, that it shrank to 4gb, which I then Win32 Disk Imager to a 16GB SD, upon boot up it then expanded to the new capacity and re-booted fine. Thanks Again


Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More