Removing old linux kernel packages

This works on dpkg based systems, so Ubuntu, Debian etc.

Now, don’t do this without knowing exactly what you’re in for. If you don’t know what kernel version you’re running or how to back up everything before you do it, please don’t come here complaining you broke your toys.

This is the final 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

I’d probably recommend running this first:

uname -r

This will tell you the current kernel version that you’re running.

Make a backup of the kernel versions:

sudo cp /boot/*$(uname -r)* ~/

Then run the command without the removal to see what you’ll be doing:

dpkg -l 'linux-*' | sed '/^ii/!d;/'"$(uname -r | sed "s/\(.*\)-\([^0-9]\+\)/\1/")"'/d;s/^[^ ]* [^ ]* \([^ ]*\).*/\1/;/[0-9]/!d'

This will show you the packages that the system will be removing. If you’re sure the list is right, then add the final part and clean up your boot directory. 🙂

Information originally found here on askubuntu. Copied and cleaned up for my own notes.

#Debian #kernel #Linux #Ubuntu