#!/bin/sh if [ $UID -ne 0 ]; then echo "This script must be ran as root." exit 1 fi IGNORE_LIST="linux-image-generic" KERNEL_LIST=$(dpkg --get-selections | grep ^linux-image- | cut -f1 | grep -Ev "^($IGNORE_LIST)" | cut -d- -f3-) # Needed in case latest kernel isn't the currently running one. LATEST_KERNEL=$(echo "$KERNEL_LIST" | sort -V | tail -n1) OLD_KERNELS=$(echo "$KERNEL_LIST" | grep -Ev "^($LATEST_KERNEL|$(uname -r))") if [ -n "$OLD_KERNELS" ]; then UNINSTALL= for k in $OLD_KERNELS; do UNINSTALL="$UNINSTALL linux-image-$k linux-headers-$k"; done dpkg -P $UNINSTALL fi