Sebastian Mogilowskis Blog

Just another blog about administration, linux and other stuff

Remove old kernel packages from Ubuntu

I blog this because the original blog doesn’t exists any longer.

I want to remove all old kernel packages from my system ( Ubuntu Linux ). I found this command:


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

But you shouldn’t use linux commands from the internet which you didn’t understand. The above one looks very cryptic. Let’s splitt it in its parts. (They are separated by ” | “)

The last part is the most important. It requires sudo and purges the packages. The “xargs” shows that the previous output is a list which is send so the command after the xargs command. This command will be executed for each element of the list.(sudo apt-get -y purge) (http://unixhelp.ed.ac.uk/CGI/man-cgi?xargs)

The commands before are two “sed” commands. This commands only filter/transform the previous output which is an “dpkg -l” that gives a list of packages. (http://unixhelp.ed.ac.uk/CGI/man-cgi?sed)

The conclusion is the first command gives a list of packages, the second and third one filter/transform the list and the last one removes the packages. We can execute the first three commands without any effect and without sudo.


$ dpkg -l 'linux-*' | sed '/^ii/!d;/'"$(uname -r | sed "s/\(.*\)-\([^0-9]\+\)/\1/")"'/d;s/^[^ ]* [^ ]* \([^ ]*\).*/\1/;/[0-9]/!d'
linux-headers-3.11.0-12
linux-headers-3.11.0-12-generic
linux-headers-3.11.0-13
linux-headers-3.11.0-13-generic
linux-headers-3.11.0-14
linux-headers-3.11.0-14-generic
linux-headers-3.11.0-15
linux-headers-3.11.0-15-generic
linux-headers-3.11.0-17
linux-headers-3.11.0-17-generic
linux-headers-3.11.0-18
linux-headers-3.11.0-18-generic
linux-image-3.11.0-12-generic
linux-image-3.11.0-13-generic
linux-image-3.11.0-17-generic
linux-image-3.11.0-18-generic
linux-image-extra-3.11.0-17-generic
linux-image-extra-3.11.0-18-generic
linux-signed-image-3.11.0-18-generic

Ok this is the list of packages wich should removed.
This are all kernels below the currently running. If you look at the second sed you see the “uname -r” command.


$ uname -r
3.11.0-19-generic

This command returns the currently running kernel version. The second sed command removes it from the list.

,

One thought on “Remove old kernel packages from Ubuntu

Leave a Reply

Your email address will not be published. Required fields are marked *

eMail-Benachrichtigung bei weiteren Kommentaren.
Auch möglich: Abo ohne Kommentar.