Skip to content

Instantly share code, notes, and snippets.

@generalov
Last active December 16, 2015 11:48
Show Gist options
  • Save generalov/5429432 to your computer and use it in GitHub Desktop.
Save generalov/5429432 to your computer and use it in GitHub Desktop.
Purge old Ubuntu kernels
#!/bin/dash
SUFFIX=$(uname -r | sed "s/^\(.*\)-\([^0-9]\+\)$/\2/")
GENERIC_VERSION=$(dpkg -l linux-$SUFFIX | awk '/^ii/{print $3}' | sed 's#^\([0-9]\+\)\.\([0-9]\+\)\.\([0-9]\+\)\.\([0-9]\+\)\..*$#\1.\2.\3-\4#g' )
CURRENT_VERSION=$(uname -r | sed "s/^\(.*\)-\([^0-9]\+\)$/\1/")
dpkg -l 'linux-image-*' 'linux-headers-*' \
| grep -v 'linux-image-'$SUFFIX | grep -v 'linux-headers-'$SUFFIX \
| grep -v $GENERIC_VERSION | grep -v $CURRENT_VERSION \
| awk '/^ii/{print $2}' \
| xargs sudo apt-get -y purge
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment