pastebin - collaborative debugging tool
kpaste.net RSS


bash script to uninstall old kernels (dpkg)
Posted by Admin on Wed 20th Jul 2011 11:45
raw | new post

  1. #!/bin/sh
  2.  
  3. if [ $UID -ne 0 ]; then
  4.         echo "This script must be ran as root."
  5.         exit 1
  6. fi
  7.  
  8. IGNORE_LIST="linux-image-generic"
  9. KERNEL_LIST=$(dpkg --get-selections | grep ^linux-image- | cut -f1 | grep -Ev "^($IGNORE_LIST)" | cut -d- -f3-)
  10.  
  11. # Needed in case latest kernel isn't the currently running one.
  12. LATEST_KERNEL=$(echo "$KERNEL_LIST" | sort -V | tail -n1)
  13. OLD_KERNELS=$(echo "$KERNEL_LIST" | grep -Ev "^($LATEST_KERNEL|$(uname -r))")
  14.  
  15. if [ -n "$OLD_KERNELS" ]; then
  16.  
  17.         UNINSTALL=
  18.  
  19.         for k in $OLD_KERNELS; do UNINSTALL="$UNINSTALL linux-image-$k linux-headers-$k"; done
  20.  
  21.         dpkg -P $UNINSTALL
  22. fi

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}




All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at