Removing Superfluous Kernels in Ubuntu 10.04

Mostly I have moved on to Ubuntu 14.04, but I do still have a couple of old 10.04 machines on my network.  The newer versions seem to be doing a great job of cleaning out the old kernels and only leaving me a small number (five?) of kernels in case I do need to roll back for any reason.

(I have needed to roll back a kernel but it was on my 10.04 system when an upgrade broke, well, everything due to something related to Compiz.  That’s another story though.)

I noticed that my 10.04 system had a couple of dozen old kernels listed in grub, and that just looks sloppy to me.  I wanted to remove them.

I poked around Google a bit and came across this excellent article which walked me through the process of locating and removing old kernels:

Remove Old Kernels in Ubuntu with One Command

The article, however, removes all but the current kernel.  I wanted a bit more of a safety net than that on hand, so I opted to alter the command to leave a small number behind instead.

Here is my final code:

dpkg -l linux-* | awk ‘/^ii/{ print $2}’ | grep -v -e “2.6.32-6″[0-5] | grep -e [0-9] | grep -E “(image|headers)” | xargs sudo apt-get -y purge

As you can see the important change is in the first grep command.  Instead of excluding the current kernel by running uname -r, I opted to exclude a group of kernels based on their version numbers (60-65).  I could have added a second grep command immediately following to exclude some of the fifties if I had wanted to exclude more.  This is how that might have looked:

dpkg -l linux-* | awk ‘/^ii/{ print $2}’ | grep -v -e “2.6.32-6″[0-5] | | grep -v -e “2.6.32-5″[8-9] |grep -e [0-9] | grep -E “(image|headers)” | xargs sudo apt-get -y purge

This version would have added an exclusion for both 58 and 59 (thus excluding 58-65).

These old 10.04 systems won’t be running much longer here (I have plans to upgrade them both to 14.04 soon enough).  Hope this helps you nonetheless.


Leave a Reply

Your email address will not be published. Required fields are marked *