Remove old kernels automatically

Tom H tomh0665 at gmail.com
Wed Oct 15 14:23:48 UTC 2014


On Wed, Oct 15, 2014 at 1:44 AM, Tom H <tomh0665 at gmail.com> wrote:
> On Mon, Oct 13, 2014 at 8:59 AM, Andrew Langhorn <andrew at ajlanghorn.com> 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?
>
> Your sed command is painful to read! :)
>
> The kernel package corresponding to the currently-loaded kernel is
> "linux-image-$(uname -r)".
>
> You can list the installed kernels with "ls -1t /boot/vmlinuz*" and
> list the corresponding packages with "ls -t /boot/vmlinuz-* | cut
> -d'-' -f2,3,4 | awk '{print "linux-image-"$0}'". Assuming that you're
> booted using the latest kernel, you can exclude the first two from
> your purge.

"ls -t /boot/vmlinuz-* | cut -d'-' -f2- | awk '{print
"linux-image-"$0}'" is better in case there are more than 4 fields in
the vmlinuz name.




More information about the ubuntu-users mailing list