Radu Cotescu's professional blog

g33k w17h pa45510n

Bash One-liner for Removing Old Kernel Artefacts

Since now I have 4 virtual machines to administer for my dissertation project, I have to find ways to do sysadmin stuff as quickly as possible and to concentrate my efforts on software development. If you ever did a kernel update you would know that a kernel upgrade will not remove the old kernel artefacts, specifically the old headers and the Linux image. While this is a good technique for avoiding incompatibilities of some binaries with the new kernel versions, therefore allowing you to test that everything is working as expected, old kernel images occupy an important amount of space (around 256 MB / release). When your system has a limited amount of storage allocated to it, this might become a problem.

The process of removing the unused images requires a few keystrokes and some user input, needing a great deal of attention not to remove the currently running kernel or - in case you are extremely inattentive - all the kernel images from your system. To semi-automate this task, you can use the following Bash one-liner:

sudo apt-get purge $(dpkg -l | grep "^ii" | awk '{print $2}' | egrep -e "^linux-headers-[0-9]|^linux-image-[0-9]" | egrep -v `uname -r | cut -d"-" -f1,2`)

Run this line only after you have booted into your new kernel, otherwise you’ll remove the upgrade.

How To, Linux, Ubuntu

« Facebook, Pidgin and the "Not authorised" error message Why awk is better than cut »