Make sure to set the variables DEVICE
and IMAGE_FILE
according to your needs.
# check the disks
sudo fdisk -l
# example
IMAGE_FILE=/home/$USER/Desktop/pi-init-`date +%Y%m%d`.img
DEVICE=sdX
sudo umount /dev/$DEVICE?*
sudo dd if=/dev/$DEVICE of=$IMAGE_FILE bs=4M status=progress
sudo chown $USER:$USER $IMAGE_FILE
# download pishrink.sh
wget https://raw.githubusercontent.com/Drewsif/PiShrink/master/pishrink.sh -O /tmp/pishrink.sh
chmod +x /tmp/pishrink.sh
# -z: gzip compress image after shrinking
# -p: remove logs, apt archives, dhcp leases and ssh hostkeys
sudo /tmp/pishrink.sh -z $IMAGE_FILE
sudo chown $USER:$USER $IMAGE_FILE.gz
# .img
sudo dd if=/dev/$IMAGE_FILE of=/dev/$DEVICE bs=4M conv=fsync status=progress
# .zip
unzip -p $IMAGE_FILE | sudo dd of=/dev/$DEVICE bs=4M conv=fsync status=progress
# .gz
gunzip -c $IMAGE_FILE | sudo dd of=/dev/$DEVICE bs=4M conv=fsync status=progress