Skip to content

Instantly share code, notes, and snippets.

@marxjohnson
Last active May 27, 2021 11:54
Show Gist options
  • Save marxjohnson/5f0f0607255951ee0e7a to your computer and use it in GitHub Desktop.
Save marxjohnson/5f0f0607255951ee0e7a to your computer and use it in GitHub Desktop.
Delete old kernel on debian/ubuntu
#!/bin/bash
# 1. Find all the packages that have installed something in /boot
# 2. Trim it down to just the kernel packages (e.g. remove memtest)
# 3. Sort them oldest->newest
# 4. Remove the newest (so we've got the updated kernel to boot into)
# 5. Remove the current kernel (so we can go back to that if the new one fails)
# 6. Pass the package list to apt-get remove for uninstallation
# Note: This code will only simulate removal. Remove -s flag from apt-get to do it for real.
dpkg -S '/boot/*' | cut -d: -f1 | grep linux-image | sort -V | uniq | head -n -1 | grep -v $(uname -a | awk '{print $3}') | xargs sudo apt-get remove -y -s --purge
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment