Remove old kernels automatically

Marius Gedminas marius at pov.lt
Tue Oct 14 11:50:04 UTC 2014


On Mon, Oct 13, 2014 at 12:59:57PM +0000, Andrew Langhorn wrote:
> 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?

You could readlink /vmlinuz.old and scrape the version number from
there.

I suggest doing the same for /vmlinuz, since the currently running
kernel is not necessarily the *latest* kernel, e.g. if you haven't
rebooted yet.

BTW in recent Ubuntu versions old kernel packages are marked as
autoremovable so you can get rid of them by doing `sudo apt-get
autoremove` every now and then.

Marius Gedminas
-- 
Science is to computer science as hydrodynamics is to plumbing.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 190 bytes
Desc: Digital signature
URL: <https://lists.ubuntu.com/archives/ubuntu-users/attachments/20141014/de8af6e9/attachment.sig>


More information about the ubuntu-users mailing list