Many Ubuntu users who upgraded Linux kernel to newer version might encounter something messy in GRUB menu. The old kernel is still there and appears on GRUB menu although we don't use it because we already have a newer version to boot. So, today I'd like to share my old post on TahuTEK.net about simple script to remove unused old kernel on your system automatically. Although this is an old post, it still works until today. I have been using it since I've moved to XFCE.


As shown in the figure above, there is an older kernel in the GRUB menu (2.6.32-21) although we have installed a newer version of kernel (2.6.32-22). To remove the old kernel, simply create a shell script as follow:
  • #/bin/bash
  • ls /boot/ | grep vmlinuz | sed 's@vmlinuz-@linux-image-@g' | grep -v `uname -r` > /tmp/kernelList
  • for I in `cat /tmp/kernelList`
  • do
  • apt-get remove $I
  • done
  • rm -f /tmp/kernelList
  • update-grub
Copy the above script into your favorite text editor and save it. For example, I give "clean.sh" as filename of the script.

Before executing the script you need to add "execute" permission to the file:
  • chmod +x clean.sh
Now, whenever you want to remove old kernel, simply execute the script with root privileges:
  • sudo ./clean.sh
For your information, you can also use Ubuntu Tweak to perform such a task. But, in any condition, you don't have Ubuntu Tweak installed on your system, you can use this simple script to remove old kernel.