Removing obsolete kernel versions

A couple of days ago I ran into a problem with an Ubuntu server I use. It has a fairly small root partition, but after an update it seemed I ran out of disk space. As it turns out, when updating to a new kernel some older files are not properly removed when using the sudo apt-get autoremove command.

Luckily, with a few command line tricks the obsolete files can be removed. The first command will just tell you which files can be removed, the second command will perform the actual cleanup.

dpkg -l linux-* | awk '/^ii/{ print $2}' | grep -v -e `uname -r | cut -f1,2 -d"-"` | grep -e [0-9] | xargs sudo apt-get --dry-run remove
dpkg -l linux-* | awk '/^ii/{ print $2}' | grep -v -e `uname -r | cut -f1,2 -d"-"` | grep -e [0-9] | xargs sudo apt-get -y purge
comments powered by Disqus