Unless you have a totally fresh install of Ubuntu, you have probably noticed that each time you boot up, the GRUB boot menu lists a bunch of previous Linux kernels which you can boot into. While this can occasionally come in handy – like if you can’t boot into the new kernel after an upgrade – those previous kernels, images and modules are usually just wasting space.
While you can go into Synaptic, search for all the bits and pieces of previous kernels, and mark them for removal, here is a much easier method. In a terminal, simply paste the following command, and it will remove all but the current kernel (if you’ve upgraded your system, or had an update with a new kernel, please reboot your machine before running this):
dpkg -l 'linux-*' | sed '/^ii/!d;/'"$(uname -r | sed "s/\(.*\)-\([^0-9]\+\)/\1/")"'/d;s/^[^ ]* [^ ]* \([^ ]*\).*/\1/;/[0-9]/!d' | xargs sudo apt-get -y purge
You will see some info about what is going on:
The following packages will be REMOVED:
linux-headers-2.6.35-22* linux-headers-2.6.35-22-generic*
linux-headers-2.6.35-23* linux-headers-2.6.35-23-generic*
linux-image-2.6.32-25-generic* linux-image-2.6.35-22-generic*
linux-image-2.6.35-23-generic*
0 upgraded, 0 newly installed, 7 to remove and 13 not upgraded.
After this operation, 586MB disk space will be freed.
(Reading database … 261863 files and directories currently installed.)
Removing linux-headers-2.6.35-22-generic …
Removing linux-headers-2.6.35-22 …
Removing linux-headers-2.6.35-23-generic …
Removing linux-headers-2.6.35-23 …
Removing linux-image-2.6.32-25-generic …
It will then go on to generate a new GRUB menu, and when you reboot, you’ll see only the current kernel is listed.
[...] pm TTC Shelbyville IT Department Leave a comment Go to comments There is an excellent article on Ubuntu Genius on how to clean Ubuntu’s previous kernals. While I have been using Linux, Unix and every [...]
Use ubuntu tweak much easier way
A “much easier way” than pasting a command in a terminal and watching it do its stuff? Methinks you jest! The process can be running before Ubuntu Tweak even finishes loading, so (call me crazy but) it would seem the terminal is not only WAY easier, but much, MUCH faster. But, hey, each to their own…
PS: Besides, everyone has a terminal – not everyone has Ubuntu Tweak (I installed it, but never actually use it).
the only reason i told was for new users and ubuntu tweak does many thing than removing old kernels.
This is entirely user choice which one they feel easy
Hey, no worries, it’s just that you DID use the words “much easier way”, rather than something like “For those scared of the terminal (or just long, strange commands), Ubuntu Tweak can do it too, and is useful in many others ways besides”. I’ll actually be doing an article on it later. I’ve been installing it since the beginning, and it has come a long way (only used to have a handful of features). You actually made me contemplate adding the U.T. method to the article, but will do a separate one and link them together. Thanks once again for your valued input!
Thanks! Just what I was looking for!
+1 to raised eyebrow comment about it being “much easier” than copying & pasting.
Wha?!
hi there !
thank you,
i just want to know if i can remove only ‘kernel headers’ and leave ‘linux-images’ ?
please help :-)
Not sure why you’d want to keep those after getting rid of the rest, but this should work:
dpkg -l ‘linux-*’ | sed ‘/^ii/!d;/’”$(uname -r | sed “s/\(.*\)-\([^0-9]\+\)/\1/”)”‘/d;s/^[^ ]* [^ ]* \([^ ]*\).*/\1/;/[0-9]/!d’ | grep “linux-headers” | xargs sudo apt-get -y purge
WordPress will probably autoformat some of that, so replace any single and double “smart” quotes (that are angled instead of vertical).
Yeah, overtype the curved and/or angled quotes or the command is useless. Sorry I haven’t got time to replace them all with code that will force them to stay as they should be, but as I said just select any single-quote and replace it with a fresh one, and some for double-quotes, THEN paste it in the terminal. Cheers.
Works great! Thanks!
I removed the header and linux-images but still I get the old entries in the list, although they start up nothing when selected and I fall back into grub. Can someone help me to remove these entries!
(Sorry for the late reply): OK, depends on whether you are using GRUB2, or still using legacy GRUB. You should find plenty of user guides (including the official ones) for both versions, and that should be one of the topics covered. In the original GRUB, it was as easy as editing a text file, but GRUB2 now stores all the info in various files, and most can’t just be simply edited like before.
Care to explain the regex for us students?
Explain all of THAT, hehe? I’d rather not (that is one long expression). Needless to say it works, which is the important part, hehe. Have a great New Year. Cheers.
Excellent site, have shared some information about your Newbies for my students.
Steve at TTC Shelbyville
Thanks!
bravo!
Worked perfectly, liberating a total of almost 700MB!!
Which I really needed, by the way, like the deserts miss the rain.
Thanks genius!
Never run this if you haven’t reboot the machine first. The script will delete the newer kernels installed since the last reboot (because of the uname -r).
Thanks, but I think most people would reboot after an upgrade or update, which is why everyone is happily reporting success with this. But thanks for pointing that out anyway.
[...] Para hacer esto podéis utilizar ubuntu tweak por ejemplo, pero me ha entrado la curiosidad sobre cómo hacerlo por consola para tener un pequeño script que lo haga, y buscando he encontrado esta web http://ubuntugenius.wordpress.com/2011/01/08/ubuntu-cleanup-how-to-remove-all-unused-linux-kernel-he… [...]