Remove all unused kernels with apt-get

aptitude remove $(dpkg -l|awk '/^ii linux-image-2/{print $2}'|sed 's/linux-image-//'|awk -v v=`uname -r` 'v>$0'|sed 's/-generic//'|awk '{printf("linux-headers-%s\nlinux-headers-%s-generic\nlinux-image-%s-generic\n",$0,$0,$0)}')
Note the double space: "...^ii␣␣linux-image-2..." Like 5813, but fixes two bugs: [1]This leaves the meta-packages 'linux-headers-generic' and 'linux-image-generic' alone so that automatic upgrades work correctly in the future. [2]Kernels newer than the currently running one are left alone (this can happen if you didn't reboot after installing a new kernel). I'm bummed that this took 228 characters. I'd like to see a simpler version.
Sample Output
The following packages will be REMOVED:  
  linux-headers-2.6.32-24 linux-headers-2.6.32-24-generic linux-headers-2.6.32-25 linux-headers-2.6.32-25-generic linux-headers-2.6.35-22 
  linux-headers-2.6.35-22-generic linux-image-2.6.32-24-generic linux-image-2.6.32-25-generic linux-image-2.6.35-22-generic 
0 packages upgraded, 0 newly installed, 9 to remove and 0 not upgraded.
Need to get 0B of archives. After unpacking 566MB will be freed.

By: __
2010-12-11 11:38:15

11 Alternatives + Submit Alt

What Others Think

worked fine on my ubuntu 10.10 it freed 586MB it started uninstalling right away without asking though. this seems dangerous!! you should use uname -a before you start maybe someone could add a confirm question?
rubo77 · 605 weeks and 1 day ago
the one that is voted quite good here: aptitude remove $(dpkg -l|awk '/^ii linux-image-2/{print $2}'|sed 's/linux-image-//'|awk -v v=`uname is not working on mine: it tries to uninstall the actual kernel i use as well !!!!!!VERY DANGEROUS!!!!!!!! use uname -a before to see what you are doing!!!
rubo77 · 596 weeks and 2 days ago
Achtung! Der Befehl #aptitude remove $(dpkg -l|egrep '^ii linux-(im|he)'|awk '{print $2}'|grep -v `uname -r`) funktioniert auch nicht!!!! bei mir versucht er den aktuellen kernel zu deinstallieren!
rubo77 · 590 weeks and 4 days ago
Have you ever encountered a problem that seemed out of your control? And how do you deal with them? Have you really focused on entertainment? Or do you just take a break from work and still think about it? The Impossible Game will be the solution to help you forget everything to have a time of real entertainment, and also a way for you to know where your limits are!
KevinDurant · 41 weeks and 6 days ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this? is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.


Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: