Remove unused Ubuntu Image Header CLI

Every time Ubuntu installs a new Linux kernel, the old one is left behind. You
have probably noticed that each time you boot up, the GRUB boot menu lists a
bunch of previous Linux kernels which you can boot into. This means that if you
are regularly updating an Ubuntu system the Grub boot menu becomes longer and
longer with kernels you donât need anymore.

The old kernels are deliberately left installed and on the menu so you can boot
a previous kernel if you have trouble with a new one. But if the new one works,
you can safely uninstall the old kernel, which will also result in the Grub menu
being cleaned up.

While you can go into Synaptic, search for all the bits and pieces of previous
kernels, and mark them for removal, here is a much easier method.

1. In a terminal, simply paste the following command, and it will remove all but
the current kernel:
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

2. Then go on to generate a new GRUB menu :
sudo update-grub

when you reboot, youâll see only the current kernel is listed.