Skip to content

Instantly share code, notes, and snippets.

@raphaeldussin
Created April 28, 2017 02:54
Show Gist options
  • Save raphaeldussin/2800e85c6c5bcfb5f0208c28e6dc2624 to your computer and use it in GitHub Desktop.
Save raphaeldussin/2800e85c6c5bcfb5f0208c28e6dc2624 to your computer and use it in GitHub Desktop.
purge ubuntu old kernels
#!/bin/bash
sudo apt-get update
sudo apt-get autoclean
sudo apt-get clean
sudo apt-get autoremove
kernel_pkgs=$( dpkg -l | grep -E 'linux-image-[0-9]' | awk '{ print $2 }' )
current_kernel=$( uname -r )
for pkg in $kernel_pkgs ; do
version=$( echo $pkg | sed -e "s/linux-image-//" )
pkg_extra=$( echo $pkg | sed -e "s/linux-image-/linux-image-extra-/" )
if [ ! $version == $current_kernel ] ; then
echo delete kernel $pkg
sudo dpkg --purge $pkg
sudo dpkg --purge $pkg_extra
sudo rm -Rf /usr/src/linux-headers-$version
sudo rm -Rf /usr/src/linux-headers-${version}-generic
else
echo keeping current kernel $pkg
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment