Skip to content

Instantly share code, notes, and snippets.

@TiGR
Last active December 4, 2015 08:11
Show Gist options
  • Save TiGR/1f93cdeb830b29e9041c to your computer and use it in GitHub Desktop.
Save TiGR/1f93cdeb830b29e9041c to your computer and use it in GitHub Desktop.
Script to remove old kernels
#!/bin/sh
# IMPORTANT: in order for this script to work as expected you need to make sure that
# kernel metapackage (such as linux-image-generic or linux-image-lts-* is installed).
# Also, since this script uses apt-get autoremove, make sure that it won't remove any
# of packages you need.
#
# How it works: it just marks all kernel packages (except packages for current kernel)
# as automatically installed, that is, available for autoremove. Then it does the
# autoremove.
#
# Latest version of this script: https://gist.github.com/TiGR/1f93cdeb830b29e9041c
# mark all installed kernels as installed automatically
aptitude markauto -q "~i~n^linux-(image|headers)-.*[0-9]"
# mark currently running kernel as installed manually. We do this with unmarkauto,
# and not by filtering out current kernel in above command just in case it happens so
# that current kernel was already marked as installed automatically some time ago. So,
# we have to make sure that it won't get removed.
aptitude unmarkauto -q "~i~n^linux-.*$(uname -r)"
# do the autoremove.
apt-get autoremove -y
@TiGR
Copy link
Author

TiGR commented Jan 18, 2015

Or, as a one-liner:

sudo sh -c 'aptitude markauto -q "~i~n^linux-(image|headers)-.*[0-9]" && aptitude unmarkauto -q "~i~n^linux-.*$(uname -r)" && apt-get autoremove -y'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment