Bash one-liner to delete only old kernels
I've seen lots of threads on how to free space on the /boot partition and that is my objective as well. However, I'm only interested in deleting old kernels and not each one of them but the current one.
I need the solution to be a one-liner since I'll be running the script from Puppet and I don't want to have extra files lying around. So far I got the following:
dpkg -l linux-* | awk '/^ii/{print $2}' | egrep [0-9] | sort -t- -k3,4 --version-sort -r | sed -e "1,/$(uname -r | cut -f1,2 -d"-")/d" | grep -v -e `uname -r | cut -f1,2 -d"-"` | xargs sudo apt-get -y purge
To be more precise, what it does at the moment is the following:
- List all the linux-* packages and print their names.
- Only list the ones that have numbers and sort them, returning the reverse result. This way, older kernels are listed last.
- Print only the results that go after the current kernel
- Since there are some linux-{image,headers} results, make sure I won't purge anything related to my current kernel
- Call apt to purge
This works, but I'm sure the solution can be more elegant and that it's safe for a production environment, since at least 20 of our servers run Ubuntu.
Thanks for your time, Alejandro.
Looks nice enough, just a few comments. The two first comments make the command safer, while the third and fourth make it a bit shorter. Feel free to follow or ignore any one of them. Though I will strongly advise to follow the first two. You want to make sure it's as safe as possible. I mean seriously. You're throwing a sudo apt-get -y purge
at some automatically generated package list. That is so evil! :)
-
Listing all
linux-*
will get you many false positives, such as (example from my output)linux-sound-base
. Even though these may be filtered out later by the rest your command, I would personally feel safer not listing them in the first place. Better control what packages you want to remove. Don't do things that may have unexpected results. So I would start out withdpkg -l linux-{image,headers}-*
-
Your regex to "list only the ones that have numbers" is slightly too simple in my opinion. For instance, there is the package
linux-libc-dev:amd64
when you're on a 64-bit system. Your regex will match. You don't want it to match. Admittedly, if you followed my first advice, thenlinux-libc-dev:amd64
won't get listed anyway, but still. We know more about the structure of a version number than the simple fact "there's a number". Additionally, it's generally a good idea to quote regexes, just to prevent potential misinterpretations by the shell. So I would make that egrep commandegrep '[0-9]+\.[0-9]+\.[0-9]+'
-
Then there is this sorting thing. Why do you sort? Since you're going to remove all kernels (except the current one) anyway, is it important for you to remove older ones before newer ones? I don't think it makes any difference. Or are you only doing that so you can then use
sed
to "Print only the results that go after the current kernel"? But IMO this feels much too complicated. Why not simply filter out the results corresponding to your current kernel, as you are already doing withgrep -v
anyway, and be done? Honestly, if I take the first part of your command (with my two previous suggestions integrated), on my machine I get$ dpkg -l linux-{image,headers}-* | awk '/^ii/{print $2}' | egrep '[0-9]+\.[0-9]+\.[0-9]+' | sort -t- -k3,4 --version-sort -r | sed -e "1,/$(uname -r | cut -f1,2 -d"-")/d" | grep -v -e `uname -r | cut -f1,2 -d"-"` linux-image-3.8.0-34-generic linux-image-3.5.0-44-generic
Removing that sorting/sed stuff, I get
$ dpkg -l linux-{image,headers}-* | awk '/^ii/{print $2}' | egrep '[0-9]+\.[0-9]+\.[0-9]+' | grep -v -e `uname -r | cut -f1,2 -d"-"` linux-image-3.5.0-44-generic linux-image-3.8.0-34-generic linux-image-extra-3.5.0-44-generic linux-image-extra-3.8.0-34-generic
So your more complicated command would actually miss two packages on my machine, that I would want to remove (now it's possible that those
linux-image-extra-*
thingys depend on thelinux-image-*
thingys and therefore would get removed anyway, but it can't hurt to make it explicit). At any rate, I don't see the point of your sorting; a simplegrep -v
without fancy preprocessing should be fine, presumably even better. I am a proponent of the KISS principle. It will make it easier for you to understand or debug later. Also, without the sorting it's slightly more efficient ;) -
This is purely aestethic but you will get the same output with this slightly shorter variant. :-)
$ dpkg -l linux-{image,headers}-* | awk '/^ii/{print $2}' | egrep '[0-9]+\.[0-9]+\.[0-9]+' | grep -v $(uname -r | cut -d- -f-2) linux-image-3.5.0-44-generic linux-image-3.8.0-34-generic linux-image-extra-3.5.0-44-generic linux-image-extra-3.8.0-34-generic
Consequently, I end up with the simpler and safer command
$ dpkg -l linux-{image,headers}-* | awk '/^ii/{print $2}' | egrep '[0-9]+\.[0-9]+\.[0-9]+' | grep -v $(uname -r | cut -d- -f-2) | xargs sudo apt-get -y purge
Since you actually want to clean up your /boot
partition, a completely different approach would be to list the contents of /boot
, use dpkg -S
to determine the packages that the individual files belong to, filter out those that belong to the current kernel, and remove the resulting packages. But I like your approach better, because it will also find outdated packages such as linux-headers-*
, which do not get installed to /boot
, but to /usr/src
.
I wrote this script that removes "linux-*" packages that have lesser version than the currently booted one. I think it is not necessary to test package status. The command asks for confirmation before purging packages. If you don't want that, add -y option to the apt-get command.
sudo apt-get purge $(dpkg-query -W -f'${Package}\n' 'linux-*' |
sed -nr 's/.*-([0-9]+(\.[0-9]+){2}-[^-]+).*/\1 &/p' | linux-version sort |
awk '($1==c){exit} {print $2}' c=$(uname -r | cut -f1,2 -d-))
However, to be able to leave configurable amount of spare kernels, I recommend to use my linux-purge
script with --keep
option. See here for more information about the script.
TL;DR: skip to the bottom.
It IS a little bit longer though. I'll break it down for you:
-
dpkg -l linux-{image,headers}-* | awk '/^ii/{print $2}'
Just like Malte suggested. Lists the relevant kernel files. -
egrep '[0-9]+\.[0-9]+\.[0-9]+'
Also suggested by Malte as the safer way to pick out only the kernel files by looking for a version number. -
Since we now are possibly listing both the image and the header packages, the package naming can vary so we have this awk workaround which is necessary for the sort
awk 'BEGIN{FS="-"}; {if ($3 ~ /[0-9]+/) print $3"-"$4,$0; else if ($4 ~ /[0-9]+/) print $4"-"$5,$0}'
The result is a new column with the version number before the original package name like below:$ dpkg -l linux-{image,headers}-* | awk '/^ii/{print $2}' | egrep '[0-9]+\.[0-9]+\.[0-9]+' | awk 'BEGIN{FS="-"}; {if ($3 ~ /[0-9]+/) print $3"-"$4,$0; else if ($4 ~ /[0-9]+/) print $4"-"$5,$0}' 3.11.0-23 linux-headers-3.11.0-23 3.11.0-23 linux-headers-3.11.0-23-generic 3.11.0-24 linux-headers-3.11.0-24 3.11.0-24 linux-headers-3.11.0-24-generic 3.11.0-26 linux-headers-3.11.0-26 3.11.0-26 linux-headers-3.11.0-26-generic 3.11.0-23 linux-image-3.11.0-23-generic 3.11.0-24 linux-image-3.11.0-24-generic 3.11.0-26 linux-image-3.11.0-26-generic 3.8.0-35 linux-image-3.8.0-35-generic 3.11.0-23 linux-image-extra-3.11.0-23-generic 3.11.0-24 linux-image-extra-3.11.0-24-generic 3.11.0-26 linux-image-extra-3.11.0-26-generic 3.8.0-35 linux-image-extra-3.8.0-35-generic
-
Now we must sort the list in order to prevent uninstalling any newer images than the one that is currently running.
sort -k1,1 --version-sort -r
giving us this:$ dpkg -l linux-{image,headers}-* | awk '/^ii/{print $2}' | egrep '[0-9]+\.[0-9]+\.[0-9]+' | awk 'BEGIN{FS="-"}; {if ($3 ~ /[0-9]+/) print $3"-"$4,$0; else if ($4 ~ /[0-9]+/) print $4"-"$5,$0}' | sort -k1,1 --version-sort -r 3.11.0-26 linux-image-extra-3.11.0-26-generic 3.11.0-26 linux-image-3.11.0-26-generic 3.11.0-26 linux-headers-3.11.0-26-generic 3.11.0-26 linux-headers-3.11.0-26 3.11.0-24 linux-image-extra-3.11.0-24-generic 3.11.0-24 linux-image-3.11.0-24-generic 3.11.0-24 linux-headers-3.11.0-24-generic 3.11.0-24 linux-headers-3.11.0-24 3.11.0-23 linux-image-extra-3.11.0-23-generic 3.11.0-23 linux-image-3.11.0-23-generic 3.11.0-23 linux-headers-3.11.0-23-generic 3.11.0-23 linux-headers-3.11.0-23 3.8.0-35 linux-image-extra-3.8.0-35-generic 3.8.0-35 linux-image-3.8.0-35-generic
-
Now strip out the current and newer kernel files
sed -e "1,/$(uname -r | cut -f1,2 -d"-")/d" | grep -v -e `uname -r | cut -f1,2 -d"-"`
giving us this:$ dpkg -l linux-{image,headers}-* | awk '/^ii/{print $2}' | egrep '[0-9]+\.[0-9]+\.[0-9]+' | awk 'BEGIN{FS="-"}; {if ($3 ~ /[0-9]+/) print $3"-"$4,$0; else if ($4 ~ /[0-9]+/) print $4"-"$5,$0}' | sort -k1,1 --version-sort -r | sed -e "1,/$(uname -r | cut -f1,2 -d"-")/d" | grep -v -e `uname -r | cut -f1,2 -d"-"` 3.11.0-23 linux-image-extra-3.11.0-23-generic 3.11.0-23 linux-image-3.11.0-23-generic 3.11.0-23 linux-headers-3.11.0-23-generic 3.11.0-23 linux-headers-3.11.0-23 3.8.0-35 linux-image-extra-3.8.0-35-generic 3.8.0-35 linux-image-3.8.0-35-generic
-
Now strip off the first column we added with
awk '{print $2}'
to get exactly what we want:$ dpkg -l linux-{image,headers}-* | awk '/^ii/{print $2}' | egrep '[0-9]+\.[0-9]+\.[0-9]+' | awk 'BEGIN{FS="-"}; {if ($3 ~ /[0-9]+/) print $3"-"$4,$0; else if ($4 ~ /[0-9]+/) print $4"-"$5,$0}' | sort -k1,1 --version-sort -r | sed -e "1,/$(uname -r | cut -f1,2 -d"-")/d" | grep -v -e `uname -r | cut -f1,2 -d"-"` | awk '{print $2}' linux-image-extra-3.11.0-23-generic linux-image-3.11.0-23-generic linux-headers-3.11.0-23-generic linux-headers-3.11.0-23 linux-image-extra-3.8.0-35-generic linux-image-3.8.0-35-generic
-
Now we can feed that to the package manager to automatically remove everything and reconfigure grub:
I recommend doing a dry run first (though for your scripting purposes this might not be practical if you have a large environment)
dpkg -l linux-{image,headers}-* | awk '/^ii/{print $2}' | egrep '[0-9]+\.[0-9]+\.[0-9]+' | awk 'BEGIN{FS="-"}; {if ($3 ~ /[0-9]+/) print $3"-"$4,$0; else if ($4 ~ /[0-9]+/) print $4"-"$5,$0}' | sort -k1,1 --version-sort -r | sed -e "1,/$(uname -r | cut -f1,2 -d"-")/d" | grep -v -e `uname -r | cut -f1,2 -d"-"` | awk '{print $2}' | xargs sudo apt-get --dry-run remove
Now if everything looks good go ahead and actually remove it with:
dpkg -l linux-{image,headers}-* | awk '/^ii/{print $2}' | egrep '[0-9]+\.[0-9]+\.[0-9]+' | awk 'BEGIN{FS="-"}; {if ($3 ~ /[0-9]+/) print $3"-"$4,$0; else if ($4 ~ /[0-9]+/) print $4"-"$5,$0}' | sort -k1,1 --version-sort -r | sed -e "1,/$(uname -r | cut -f1,2 -d"-")/d" | grep -v -e `uname -r | cut -f1,2 -d"-"` | awk '{print $2}' | xargs sudo apt-get -y purge
Once again the whole point of this "one-liner" is to remove only the kernels OLDER than the currently running kernel (which leaves any newly installed kernels still available)
Thanks let me know how this works for you and if you could improve it!