Remove all unused kernels with apt-get

aptitude remove $(dpkg -l|egrep '^ii linux-(im|he)'|awk '{print $2}'|grep -v `uname -r`)
This should do the same thing and is about 70 chars shorter.
Sample Output
The following packages will be REMOVED:
  linux-image-2.6.32-3-amd64 linux-image-amd64 
0 packages upgraded, 0 newly installed, 2 to remove and 0 not upgraded.
Need to get 0B of archives. After unpacking 99.6MB will be freed.
Would download/install/remove packages.

# `uname -r` = "2.6.34-dbb-deb"

11 Alternatives + Submit Alt

What Others Think

Note there's a double space after the '^ii' which isn't shown unless you click the command to cut & paste. Good command!
spaceyjase · 595 weeks and 2 days ago
Nifty, but two problems (i'm using ubuntu 10.10): [1] It removes kernels newer than the one running (which are present if you upgrade and don't reboot immediately) and [2] It removes meta-packages '/linux-(headers|image)-generic/' which causes a chain of unintended consequences (e.g., 'linux-generic' has unmet dependencies) and prevents auto-upgrading to future kernels.
__ · 573 weeks ago
i am using Ubuntu 11.4 and it tries to remove the actual kernel i am running right now!!! DANGEROUS!!!!! use #uname -a before, to see the actual kernel
rubo77 · 554 weeks and 3 days ago
Indeed, this is a bad implementation of a good idea.
assarbad · 510 weeks and 3 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: