Skip to content

Instantly share code, notes, and snippets.

@Makistos
Created February 11, 2016 13:34
Show Gist options
  • Save Makistos/abf260dc4505c548e0f4 to your computer and use it in GitHub Desktop.
Save Makistos/abf260dc4505c548e0f4 to your computer and use it in GitHub Desktop.
Remove oldest Linux kernel and associated packages if there are more than two available. #bash #linux #bash-select #kernel
#!/bin/bash
KERNELS=`ls /boot | grep vmlinuz | cut -d'-' -f2,3`
echo Available kernels: $KERNELS
KERNCOUNT=`echo $KERNELS | wc -w`
if [ $KERNCOUNT -gt 2 ];
then
OLDEST=`echo "${KERNELS%% *}" | head -1`
PACKAGES=`dpkg -l | grep ^ii |grep $OLDEST | awk -F' ' '{ print $2 }'`
echo "Oldest package (to be removed): $OLDEST"
echo These packages will be removed: $PACKAGES
echo Proceed?
PS3="Select: "
select yn in Yes No;
do
case $yn in
Yes)
echo "Proceed";break
;;
*)
exit
;;
esac
done
sudo apt-get remove -y $PACKAGES
echo Done removing oldest kernel
else
echo There are only two kernels in the system. Aborting.
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment