Sunday, March 29, 2015

Remove all old linux kernels, headers and modules for Debian based systems

This has come up often for me with my linux machines, so I'll just blog a blog on the topic.  I came across two useful posts:

RemoveOldKernels

Ubuntu Cleanup: How to Remove All Unused Linux Kernel Headers, Images and Modules

The former is useful, but it only removes the kernels.  I want to remove all headers, etc. associated with them.  So, before I run the command from the latter blog post, I just want to confirm what I'm going to remove, as should you, with the following code (note, not as root just to be even more cautious):
dpkg -l 'linux-*' | sed '/^ii/!d;/'"$(uname -r | sed "s/\(.*\)-\([^0-9]\+\)/\1/")"'/d;s/^[^ ]* [^ ]* \([^ ]*\).*/\1/;/[0-9]/!d'
 Perfect, now I can just run the one-liner from that latter blog post:

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

Boom, I just got rid of over 3 GB's of old linux kernels on my system.

No comments:

Post a Comment