Skip to content

Instantly share code, notes, and snippets.

@willprice
Created September 9, 2019 09:33
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 willprice/d09b5a800bb3b8d13888deb7f1f49594 to your computer and use it in GitHub Desktop.
Save willprice/d09b5a800bb3b8d13888deb7f1f49594 to your computer and use it in GitHub Desktop.
Remove old kernels from ubuntu
#!/bin/bash
set +ex
KEEP_LAST_N=2
OLD_KERNELS=$(dpkg -l | grep -i 'linux-image-[[:digit:]]' | awk '{ print $2 }' | sort -rV | tail -n "+$(($KEEP_LAST_N + 1))")
if [ -n "$OLD_KERNELS" ]; then
apt-get -qy remove --purge $OLD_KERNELS
fi
apt-get -qy autoremove --purge
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment