Remove old kernels automatically

Andrew Langhorn andrew at ajlanghorn.com
Mon Oct 13 12:59:57 UTC 2014


Hi,


Using sed, uname and apt, I am trying to ensure that all kernels older than the current and one behind are uninstalled from my machines. I keep the current one, for obvious reasons, and the previous kernel in case I need to roll back.


To do this, I'm using `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`, which pipes the output of `uname -r ` to sed, ignores any lines beginning with `ii` to ensure installed kernels aren't removed, and applies that to the ouput of `dpkg -l 'linux-*`.


This ensures that all previous kernels present are removed.


Can I blacklist the previous one, given that I don't know the name, as it may well change?


Thanks,


Andrew

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ubuntu.com/archives/ubuntu-users/attachments/20141013/c9bf741d/attachment.html>


More information about the ubuntu-users mailing list