0

I have upgraded my OS version a few times. What is the best way to remove the old kernels and free space?

1 Answers1

0

Having extra kernels take space up. You can look for your old kernels manually in the /usr/src folder. However, the following command can remove the linux kernels quickly and easily.

From a Terminal window, type in:

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

The above line should only be used if you know that your present kernel is working the way you want it and any other kernels you are not concerned about. This will remove all kernels but the current active one that is running on your system. It will remove even newer kernels if you are booted to an older kernel.

Terrance
  • 41,612
  • 7
  • 124
  • 183
  • 1
    If you lift a random command from teh interwebs, at list link to a source. Especially if you're going to use the -yoption. – muru Apr 22 '15 at 16:22
  • So, Terrance, If I am parsing this correctly, your dpkg and sed commands are looking for kernel instances and then calling purge to delete them? I don't like the autoremove suggestion as it seems to remove other things that no longer have dependancies, and I have loaded a number of such packages to be able to compile other software. – Scott McGee Apr 22 '15 at 16:25
  • muru, I would never randomly load a command without first understanding what it does. Yes, I know what the -y option does. Your response was interesting, but I don't want to remove other packages, just the old kernels. (oh, it is called "the web" or "the internet", not the interweb!) – Scott McGee Apr 22 '15 at 16:28
  • @ScottMcGee you are better than the average user, if you take the time to read and understand a command. "teh interwebs" is a joke. – muru Apr 22 '15 at 16:31
  • @SCottMcGee I have used this command in the past and it has freed up tons of space on my system without removing the current kernel. I can see where your concern is about blindly removing files. However, the command only returns the kernels that are presently installed on the host, minus the current kernel. Purging the packages removes extra stuff wasting space. – Terrance Apr 22 '15 at 16:43