Quickly Uninstall Old Linux Kernels With A Bash Script

We've shown you how to clean up your messy Ubuntu boot menu before, but if you'd like to avoid manually uninstalling old kernels, this handy bash script will do it all for you.

After you've had one Ubuntu installation for awhile, you start to build up a long list of old kernels installed on your system. Not only to they clutter up your boot menu, but they also take up space on your hard drive. While you could uninstall each kernel one by one from Synaptic, this bash script will uninstall them with just a quick Terminal command.

Copy the code below and paste it into a text editor, saving the file as cleanKernels.sh:

#/bin/bash ls /boot/ | grep vmlinuz | sed '[email protected]@[email protected]' | grep -v `uname -r` > /tmp/kernelList for I in `cat /tmp/kernelList` do aptitude remove $I done rm -f /tmp/kernelList update-grub

Now, whenever you feel your boot menu is getting a little messy, just open up a Terminal and run sudo bash /path/to/cleanKernels.sh to clean it up (where /path/to/ is the path to wherever you saved the script).

I tried this on my system and the script ran fine, but my current Ubuntu installation is so new that I don't have any other kernels installed right now—so try it out and let us know in the comments how it works for you!

Clean Up Your Grub Menu and the Kernels You Do Not Use [Linux Operating System via Ubuntu Forums]


Comments

    I got this when executing in Ubuntu 10.10 UNR:

    sed: -e expression #1, char 1: unknown command: `�'
    Generating grub.cfg ...
    Found linux image: /boot/vmlinuz-2.6.35-24-generic
    Found initrd image: /boot/initrd.img-2.6.35-24-generic
    Found linux image: /boot/vmlinuz-2.6.35-22-generic
    Found initrd image: /boot/initrd.img-2.6.35-22-generic
    Found memtest86+ image: /boot/memtest86+.bin
    done

    Otherwise, I think it cleaned nicely!

    Thanks!

    I got this to work with two steps:
    - Fix the quotes around '[email protected]@linux-image-@g'
    - Install 'aptitude'

    The 'unknown command' noted in a previous comment indicates it did not work.

Join the discussion!