Remove unused kernels

Apr 13, 2014 16:45 · 84 words · 1 minute read bash oneliner ubuntu kernel

Removes all but current kernel and headers. This is a note for personal use. Use at your own risk :)

dpkg-query -f '${Package}\n' -W |  egrep 'linux-(headers|image)-[[:digit:]].*(|-generic)' | grep -v $(uname -r | sed 's/-generic//') | sudo xargs apt-get -q -q -yy --purge remove


  1. List all packages.
  2. Find only linux-image and linux-header packages with version number, not the two main meta packages.
  3. Remove the linux-image and linux-header for running kernel from the list.
  4. Purge all listed packages from system without asking any questions.