Removing old kernel packages

Jonathan Marsden jmarsden at
Fri Mar 9 07:26:43 UTC 2012


On 03/08/2012 06:23 AM, Chris wrote:

> Thanks got your email! It was discussed at the meeting last night
> and this command is much more readable.

Yes, I was there in #ubuntu-meeting for the last half of the meeting,
but I was working and talking with someone else in my office (I never
did eat lunch that day, I was too busy!), so I couldn't actively

> One part of it I'm not certain about and that is the [23]\* in it.
> Wouldn't that limit it to just 2.* and 3.* kernels?

Yes, exactly.  Are there any other Linux kernels likely to show up any
time soon? :)

> What would the result be if we'd remove that [23]\ ? Other then that
> I've no issues with changing it to that on the wiki.

The [23] is really just an additional safety net.  The idea is to only
deal with packages that we really are pretty sure are
kernels/kernel-headers.  We're *removing* software from a working
computer, so a little extra care and attention can help avoid unwanted

All kernels, for a fairly long time (by Linux standards), will have
kernel version numbers that start with either a 2 or a 3.  There would
be no direct change to what the commands do if you remove the [23] but
the commands might then be slightly less safe, and in some unknown
future situation they might match a package that was not actually a
kernel, and remove it.  You could of course edit the pattern to match
any digit, if you prefer, as a half-way version between matching 2 or 3,
and not checking anything at all at that point in the package name.
I prefer leaving that [23] in there, but if you want to remove it,
that's fine too.

I suggest you leave the \ before the * alone.  Removing it could break
these commands, sometimes.  It delays expansion of the *, which matters
in some (rare, but they actually happened to me once!) circumstances...
running these commands when you have files matching the filespec in the
current directory.  You don't want the bash shell to expand the pattern
to match files you happen to have in the current directory, you want to
pass the * to the dpkg command as part of the pattern, so it will list
all the packages starting with linux-image-[23] or linux-headers-[23].
So you need the \ to protect the * from the shell.

Try doing

  touch linux-image-testfile
  dpkg -l linux-image-*

and then try

  dpkg -l linux-image-\*

and you will quickly understand why the \ matters :)

> P.s.: might be worth suggesting to add to the default bashrc of
> Ubuntu? Thoughts?

I doubt it, but I'm glad you like the commands that much!

It would be trivial to create a .deb package that drops a file into
/etc/profile.d/ containing these aliases and the function, so they would
be automatically sourced by every bash login shell.  I *think* you're
allowed to put bash-specific stuff in there, I'm not 100% sure, it might
have to be sh-compatible, and I'm not sure the function and the use of
$() would be :)  I can try creating an oldkernel-remover package, if you
really think enough people would want this!  Then, if Julien wanted to
sponsor it, we could try to get the package into Ubuntu and then into
the next Lubuntu version, if it really is useful enough to justify doing


>> alias oldkernels='dpkg -l linux-{headers,image}-[23]\* |grep ^ii
>> |grep -v $(uname -r |sed -e "s/-generic//") |cut -c 5-40'

>> alias remove-oldkernels='sudo apt-get remove $(dpkg -l

>> linux-{headers,image}-[23]\* |grep ^ii |grep -v $(uname -r |sed -e
>> "s/-generic//") |cut -c 5-40)'

>> function remove-oldkernels-except () { sudo apt-get remove $(dpkg -l
>> linux-{headers,image}-[23]\* |grep ^ii |grep -v $(uname -r |sed -e
>> "s/-generic//") |cut -c 5-40 | grep -v $1) ; }

More information about the Lubuntu-users mailing list