Skip to content

Instantly share code, notes, and snippets.

@meschbach
Created June 3, 2022 16:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save meschbach/571a6dcfd341b6d320d9b278dc92317c to your computer and use it in GitHub Desktop.
Save meschbach/571a6dcfd341b6d320d9b278dc92317c to your computer and use it in GitHub Desktop.
Ubuntu Kernel Cleanup
#/bin/bash
# Removes kernels + headers remaining on a ubuntu machine.
current_version=$(uname -a |awk '{print substr($3, 1, length($3)-length("-generic"));}')
echo "Current Kernel: $current_version"
installed=$(dpkg-query -l | grep '^ii' | awk '{print $2}')
headers=$(grep linux-headers- <<<"$installed" |head -n -3 |grep -v "$current_version")
modules=$(grep linux-modules <<<"$installed" |grep -v "extra" |head -n -2 |grep -v "$current_version")
extras=$(grep linux-modules-extra <<<"$installed" |head -n -2 |grep -v "$current_version")
echo "Removing"
echo $headers $modules $extras
set -xe
echo $headers $modules $extras | xargs sudo aptitude purge -y
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment