Ubuntu: How to permanently delete ALL older kernels using the shell with confirmation question?



Question:

To free up some disk-space you can check which kernel you are using with:

uname -r  

And then remove ALL kernel versions but the current with:

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  

You can run this command as a cron job, if you want this done automatically. However this is not wise, as you should ALWAYS have an old kernel or two to fall back to (just in case the new one doesn't work with your system). At the very least, if you've just upgraded the kernel, reboot before deleting the older versions.

Source: How do I remove old kernel versions to clean up the boot menu?

But this command will start uninstalling right away, without asking!

How can I change that line, so you are asked to continue before it uninstalls?


Solution:1

Use following command instead of what you used before:

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

Here is my output when I execute above command:

saurav@saurav-P4I45Gx-PE:~$ sudo apt-get purge $(dpkg -l 'linux-*' | sed '/^ii/!d;/'"$(uname -r | sed "s/\(.*\)-\([^0-9]\+\)/\1/")"'/d;s/^[^ ]* [^ ]* \([^ ]*\).*/\1/;/[0-9]/!d')  [sudo] password for saurav:   Reading package lists... Done  Building dependency tree         Reading state information... Done  The following packages will be REMOVED:    linux-headers-3.11.0-031100* linux-headers-3.11.0-031100-generic* linux-image-3.11.0-031100-generic*  0 upgraded, 0 newly installed, 3 to remove and 39 not upgraded.  After this operation, 212 MB disk space will be freed.  Do you want to continue [Y/n]? n  Abort.  

That's it!


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »