Remove unused kernels

· 85 words · 1 minute read

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

Breakdown 🔗

  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.