Skip to content

Instantly share code, notes, and snippets.

@bse666
Last active January 7, 2016 13:25
Show Gist options
  • Save bse666/b46a089b73c17facb2d2 to your computer and use it in GitHub Desktop.
Save bse666/b46a089b73c17facb2d2 to your computer and use it in GitHub Desktop.
Purge unused Kernels on Ubuntu
#!/bin/bash [0/1863]
# /root/ubuntu-purge-unused-kernels.sh
# crontab:
# @reboot /root/ubuntu-purge-unused-kernels.sh
dpkg --get-selections | `#show all installed packages` \
grep 'linux-image-*' | `#select all installed images` \
awk '{print $1}' | `#select only package name)` \
egrep -v "linux-image-$(uname -r)|linux-image-generic" | `#remove current and base kernel from list` \
head -n -2 | `#remove two recent kernels from list` \
sed 's/^linux-image-\(.*\)$/\1/' | \
while read n
do
echo 'Purging unneeded kernel images and headers for: '$n
sudo apt-get --yes purge linux-image-$n #purge images
sudo apt-get --yes purge linux-headers-$n #purge headers
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment