Random Linux Tip – Clearing out old kernels on Ubuntu
I get fed up occasionally of the million and one old kernels I have installed after multiple updates and upgrades of my Ubuntu install, so I have the following shell script snippet to sort it out for me:
|
1 2 3 |
<code lang="bash">
sudo aptitude -F '%p' search linux-{image,headers,restricted}-2.6~i | grep -v `uname -r | sed -r 's/-[a-z0-9A-Z]+$//'`
</code> |
That pumps out a list of all the installed kernels and associated images/modules/etc apart from the one you are currently using. It also keeps around suffixed kernels at the same version as your current one (so if you have the -pae, -rt, -smp, -generic and -preempt kernels installed, it will leave the ones that match the version number of your current kernel).
Now, if you want to be dangerous and not check the output, you can bin them all with a single command, like so:
|
1 2 3 |
<code lang="bash">
sudo aptitude -F '%p' search linux-{image,headers,restricted}-2.6~i | grep -v `uname -r | sed -r 's/-[a-z0-9A-Z]+$//'` | xargs sudo aptitude purge -y
</code> |
Et voila! Goodbye superfluous kernels!
Ralph Corderoy 10:08 am on May 10, 2011 Permalink
Don’t think the searching aptitude needs the sudo?
Dark Liquid 11:33 am on May 10, 2011 Permalink
I think you’re probably right.