Skip to content

Instantly share code, notes, and snippets.

@blizzz
Created December 5, 2022 11:16
Show Gist options
  • Save blizzz/40eb3ed73372692ac26603b35075e46b to your computer and use it in GitHub Desktop.
Save blizzz/40eb3ed73372692ac26603b35075e46b to your computer and use it in GitHub Desktop.
remove old installed linux kernel (tested on ubuntu releases)
#!/usr/bin/env bash
# ⚠️ ATTENTION! It purges kernels without asking for confirmation!!!
CURRENT_KERNEL=$(uname -r | grep -oE '[0-9\.]+-[0-9]+')
LATEST_KERNEL=$(apt show linux-image-generic | grep -oE 'linux-image-[0-9\.]+-[0-9]+' | cut -d '-' -f3-)
BASE_KERNEL=$(echo ${CURRENT_KERNEL} | cut -d '.' -f 1-2)
apt list --installed | grep linux | cut -d '/' -f 1 | egrep -v "(${CURRENT_KERNEL}|${LATEST_KERNEL})" | grep "${BASE_KERNEL}." | tr '\n' ' ' | xargs apt purge -y
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment