Skip to content

Instantly share code, notes, and snippets.

@grepwood
Created August 25, 2014 10:43
Show Gist options
  • Save grepwood/51b4eddce4de27819964 to your computer and use it in GitHub Desktop.
Save grepwood/51b4eddce4de27819964 to your computer and use it in GitHub Desktop.
This script completely purges other kernels than the current running. Useful for when you've upgraded to a newer one, it's stable, and you don't need the old one.
#!/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"
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!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment