How to remove old Linux kernel headers

As time goes by and you keep installing more and more updates on a Linux machine, it will eventually get cluttered with all the kernels it has ever left behind. Probably the only time these can come in handy is when you can’t boot into a new kernel right after an upgrade.

The following command, which really is better than it looks, will remove all the previous kernel images, headers and modules that can and will take up precious disk space, leaving only the current one intact:

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

This can be useful on a server that performs automatic system-side incremental backups to an external storage device. Running this command will help delay that “dafuq” moment when the backup sizes exceed the storage capacity of the external drive.

Now, for the sake of any impending F*CKs, imagine how dumb it would be to run this command on a system that just had an update with a new kernel and hasn’t been rebooted yet…

Running dangerous commands in Linux


5 thoughts on “How to remove old Linux kernel headers

  1. Woah! I’m really loving the template/theme of this site.
    It’s simple, yet effective. A lot of times it’s tough to get that “perfect balance”
    between superb usability and appearance. I must say that you’ve
    done a fantastic job with this. In addition, the blog loads very
    fast for me on Firefox. Excellent Blog!


    1. Well, thank you very much, you are kind, but I’m really new at this and the only credit I can take is for making the actual choices that got this blog the way it is – the rest and most of it goes to the Zoren Theme and WordPress, which is pretty awesome and fast in itself.


  2. Heya i’m for the first time here. I found this board and I to find It truly helpful & it helped me out much.
    I’m hoping to give something back and aid others like you aided me.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s