Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save imranh2/7cc6906e9d6cc94e278d to your computer and use it in GitHub Desktop.
Save imranh2/7cc6906e9d6cc94e278d to your computer and use it in GitHub Desktop.
#!/bin/bash
CURRENT_KERNEL=`uname -r`
OLD_KERNELS=`ls /usr/src/ -l | grep -v ">"| awk '{print $9}' | grep linux | grep -v $CURRENT_KERNEL | sed -r 's/^.{6}//'`
AMOUNT_OF_OLD_KERNELS=`ls /usr/src/ -l | grep -v ">" | awk '{print $9}' | grep linux | grep -v $CURRENT_KERNEL | wc -l`
echo "Amount of kernels to get rid of: $AMOUNT_OF_OLD_KERNELS"
read -r -p "Are you sure? [y/N] " response
if [[ $response =~ ^([yY][eE][sS]|[yY])$ ]]
then
for (( COUNTER=0; COUNTER<$AMOUNT_OF_OLD_KERNELS; COUNTER++ ))
do
HEAD=$((COUNTER+1))
DIR=`ls /usr/src/ -l | grep -v ">" | awk '{print $9}' | grep linux | grep -v $CURRENT_KERNEL | head -n$HEAD | tail -n1`
echo -ne "Fetching package name of kernel #$HEAD... "
# Gentoo only
PACKAGE=`equery b $DIR | head -n+1`
echo "done!"
echo "Kernel #$HEAD is currently known as $PACKAGE"
echo -ne "Removing kernel #$HEAD ..."
# Gentoo only
emerge -Cq $PACKAGE
echo "done!"
echo -ne "Removing kernel #$HEAD source directory... "
rm -rf /usr/src/$DIR
echo "done!"
# Truncating the "linux-" from directory name so that we can find it in /boot and /lib/modules
KERNEL=`echo $DIR | sed -r 's/^.{6}//'`
echo -ne "Removing kernel #$HEAD from /boot... "
rm -f /boot/*$KERNEL*
echo "done!"
echo -ne "Removing kernel #$HEAD modules... "
rm -rf /lib/modules/$KERNEL
echo "done!"
echo "$PACKAGE has been purged from the system."
done
echo "All your old kernels have been purged. Have a nice day and remember to install Gentoo!"
else
echo "Quitting. No changes made."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment