Makistos
2/11/2016 - 1:34 PM

Remove oldest Linux kernel and associated packages if there are more than two available. #bash #linux #bash-select #kernel

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