Skip to content

Instantly share code, notes, and snippets.

@CaledoniaProject
Last active October 10, 2020 02:17
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 CaledoniaProject/243e00fa6ce5866efe39287cf94f1d3c to your computer and use it in GitHub Desktop.
Save CaledoniaProject/243e00fa6ce5866efe39287cf94f1d3c to your computer and use it in GitHub Desktop.
Remove expired kernel in Ubuntu
#!/bin/bash
installed=( $(dpkg-query -W --showformat='${Package}\n' | grep -oP '^linux-image-\K\d.*' | sort -r) )
running=$(uname -r)
echo Installed versions: ${installed[@]}
if [[ ${#installed[@]} -le 1 ]]; then
echo Nothing to do
exit
fi
if [[ ${installed[0]} != $running ]]; then
echo Must reboot system to load ${installed[0]}
echo Current version $running
exit
fi
packages=
for version in ${installed[@]}
do
if [[ $version == ${installed[0]} ]]; then
continue
fi
packages="linux-headers-${version} linux-image-${version} linux-modules-${version} linux-modules-extra-${version} $packages"
done
echo Packages to uninstall: $packages
apt autoremove --purge $packages
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment