• Blog
  • Research
  • Automated Download of the Latest Version of Raspbian

    12 January, 2020 | General Hackery

    I often set up Raspberry Pis. I always like to make sure a new Pi on my network runs the latest version of the OS. I had a regular problem in that I wanted to set up a Pi quickly, but ended up having to go to the website, check for latest version, and download that before I could start writing the card image.

    Thankfully, the Raspberry Pi website always has the latest version of Raspbian, and always at the same URL which directs to the latest version. So, being very lazy, I wrote this script.


    FULL_URL=`curl -ILs -o /dev/null -w %{url_effective} https://downloads.raspberrypi.org/raspbian_latest`
    LITE_URL=`curl -ILs -o /dev/null -w %{url_effective} https://downloads.raspberrypi.org/raspbian_lite_latest`
    BASE_DIR=`echo "$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd)"`
    FILENAME=`echo $FULL_URL | sed 's#^.*/##'`
    VERSION=`echo $FILENAME | sed 's#-raspbian-.*$##'`

    cd $BASE_DIR
    WC=`ls | grep $VERSION | wc -l`
    if [ $WC -eq 0 ]
    echo Downloading $FILENAME
    wget -O /tmp/raspbian-lite.zip $LITE_URL
    wget -O /tmp/raspbian-full.zip $FULL_URL
    unzip /tmp/raspbian-lite.zip
    unzip /tmp/raspbian-full.zip
    rm /tmp/raspbian-*.zip

    The script should be put in a directory on a Linux box and called from Cron. When the script is run it connects to raspberrypi.org to determine when the latest version of Raspbian was released. If you have a file in the current directory with that date in the filename, it exits cleanly and does nothing. If you don't, it downloads the console-only and desktop versions of the latest release and unzips them for you. So the next time you need to set up a Pi, simply look in this directory and you'll always have the latest version already downloaded.