81 Comments


  1. Clears up the space on /boot and removes old kernel data from the system, as described.

    Works like a charm on Ubuntu 14 LTS x64 with crypto.

    Thank you very much for explaining what the command does and how it does it.

    This is invaluable info!

    Reply
  2. Rich

    Used the 'very long command'

    "dpkg -l linux-* | awk '/^ii/{ print $2}' | grep -v -e `uname -r | cut -f1,2 -d"-"` | grep -e [0-9] | xargs sudo apt-get -y purge"

    Worked like a charm. Plenty of space in /boot

    Reply
  3. Alessandro

    Hi to all!

    When i try the dry-run command I recive:
    The following packages have unmet dependencies:
    linux-image-generic-lts-quantal : Depends: linux-image-3.5.0-54-generic but it is not going to be installed
    E: Unmet dependencies. Try 'apt-get -f install' with no packages (or specify a solution).

    Mine kernel is 3.5.0-45, but I don't know what is the package that depend from them...
    I can't use apt-get -f install because my boot partition is full..
    Can anyone help me please ?
    Thank you very much, and apologize for my bad english!

    Reply
  4. kim jong oun

    this worked a treat. my /boot was full and giving issues on ubuntu 12.04.04 AMD 64bit. i was crapping myself trying to fix this, but the one liner worked!

    Reply
  5. john herring

    Thank you so much for this command. It worked perfectly! I had searched for hours to find a simple solution to the boot space problem.

    Reply
  6. CJ

    After confirming with "--dry-run remove" that only images and headers would be removed in my 12.04 64 bit system, I pulled the trigger.
    The last line of the result is "Do you want to continue [Y/n]? Abort." and I'm returned to the prompt as if I'd aborted the command.
    Haven't got a clue what went wrong.

    This command:
    dpkg -l linux-* | awk '/^ii/{ print $2}' | grep -v -e `uname -r | cut -f1,2 -d"-"` | grep -e [0-9] | grep -E "(image|headers)" | xargs sudo apt-get remove

    Reply
  7. P B
    Reply
    • Linerd

      Thanks for your comment.

      So, inserting a

      grep -v libc

      will fix it in the case of libc-dev:amd64. Maybe I should make it so it only includes packages that start with "linux-image" or "linux-headers", just in case some other problem package shows up. I can do that by adding

      grep -E "(image|headers)"
      Reply
      • P B

        Cheers for that. Maybe you could modify the date at the top of the page. It currently says October 2nd 2010 which gives a first impression that it's 3 years old. And under the Updates include this latest tweak.
        Regards

        Reply
  8. P B

    It is only supposed to match kernel related packages but it is also finding packages like linux-libc-dev:amd64. As it begins with linux and has a number. People could get caught out with this and
    remove things they dont want removing.

    Reply
    • Linerd

      That makes the dry run that much more important.

      Reply
  9. Augusto

    You are awesome
    You wrote this post three years ago and it is working
    thanks so much

    Reply
  10. Van Bastos

    I love you man. hahaha it really worked.

    Thanks for you very long command

    "dpkg -l linux-* | awk '/^ii/{ print $2}' | grep -v -e `uname -r | cut -f1,2 -d"-"` | grep -e [0-9] | xargs sudo apt-get -y purge"

    Reply

  11. Thanks for this helpful command! However, I have one remark:

    I also have linux-libc-dev:amd64 installed, which became a candidate for removal because of the last check "only keep items with numbers in them". Of course, including "grep -v libc" in the command solved this.

    Reply
    • Some Dummy

      Or cut -f1,1 -d":". Also it may help to pray that the Linux people choose not to use numbers in any other necessary packages.

      Reply
  12. Vitalie Ciubotaru

    a nice command line one-liner that removes all but the currently running kernel
    While this is an accurate description, it might not be what we want. Imagine the following situation:
    1. I do `sudo apt-get update` and see that a new kernel is available.
    2. I install the new kernel. I don't reboot (why would I, if everything works fine?)
    3. I realize that I have a bunch of older kernels in my system. I want to remove them.
    4. I run this one-liner. Instead of deleting everything except the newest kernel, it deletes everything except the currently running one. No gain.

    Reply
    • Linerd

      Why do you say no gain? You still got rid of all of those old kernels, didn't you? And removing the running kernel certainly seems like a bad idea. In your scenario, the next time you install updates you should get the new kernel again. Now you only have one extra kernel rather than 5 or 10.

      Reply
  13. Nik

    Thanks!

    Reply
  14. vacri

    Be careful, this will hit other packages as well - on my system (ubuntu 13.04) it also grabs "linux-libc-dev:amd64".

    ... which is pretty much what Rambo just said.

    Reply
  15. Rambo

    Hmm I think it willl work on a 64 system, im currently in the process of adjusting it a little though. The trick of picking out everything that contain numbers for processing doesnt work on 64 systems since we have filenames like *amd64 and so.

    Reply

  16. Jeremy Acton

    Please could someone explain how to type the vertical line between phrases? I don't see it on my keyboard ANYWHERE, and it's obviously not the forward or back slashes.

    Reply
    • Jeremy Acton

      don't worry abt replying . sorted.
      Thanks for the excellent one liner. It works like a magic charm.

      Reply
      • Linerd

        You're welcome. I'm glad you got it sorted out.

        Reply
  17. Jay Fude

    Still works, 12.10 and linux mint (debian) on a 64 bit system.

    Reply
  18. Larry

    This worked fine on a 64 bit system. no issues.

    Reply
  19. John K

    Found this page when I needed to do some clean up on my system which was very helpful. I did make a change for filtering the package list that I would like to pass on:

    dpkg -l | awk --assign=mykernel=$(uname -r) '$2 ~ mykernel{next}$2 ~ /^linux-image-[0-9]/ || $2 ~ /^linux-headers-[0-9]/{print $2}' | xargs sudo apt-get -y purge

    Reply
  20. Edgar Cheetham

    I have used your technique to remove old kernels and it has worked quite well in he past, and I thank you for that. However now, after upgrading from linux-headers-2.6.32-45 (2.6.32-45.103) to 2.6.32-45.104, when I type:
    :~$ dpkg -l linux-*
    I get this:
    No packages found matching linux-3.7.1.
    No packages found matching linux-3.8.0.
    What gives?

    Reply
    • Linerd

      That's really strange. I've never seen that. You could try the more manual route to see how that works out.
      http://tuxtweaks.com/2009/12/remove-old-kernels-in-ubuntu/

      Reply
      • Edgar Cheetham

        I found the answer to my own question.
        It has nothing to do with the upgrade. It has something to do with me taking the crash course on how to build and run a Linux kernel. I built two kernels (3.7.1 and 3.8.0) but never run them. They are sitting in folders with said names in my Home Folder.
        I feel kinda dumb...
        Thanks

        Reply
  21. Konstantin

    Did not work well on Ubuntu 12.10 (64bit) - it wanted to lot of extra things:

    Feb 22, 11:27:02> dpkg -l linux-* | awk '/^ii/{ print $2}' | grep -v -e `uname -r | cut -f1,2 -d"-"` | grep -e [0-9] | xargs sudo apt-get --dry-run remove
    Reading package lists... Done
    Building dependency tree
    Reading state information... Done
    The following packages were automatically installed and are no longer required:
    lib32z1 libavahi-client-dev libavahi-common-dev libgnutls-openssl27 libgnutlsxx27 libgpg-error-dev libidn11-dev libieee1284-3-dev libjbig-dev libkadm5clnt-mit8 libkadm5srv-mit8 liblzma-dev libp11-kit-dev libsensors4-dev libsnmp-perl libtasn1-3-dev libtiffxx5
    libusb++-0.1-4c2 libv4l-dev libwrap0-dev ncurses-term pax
    Use 'apt-get autoremove' to remove them.
    The following packages will be REMOVED:
    build-essential comerr-dev g++ g++-4.4 g++-4.7 google-earth-stable krb5-multidev libc6-dev libcups2-dev libcupsimage2-dev libcurl4-nss-dev libexif-dev libexpat1-dev libgcrypt11-dev libgnutls-dev libgphoto2-2-dev libjpeg-dev libjpeg-turbo8-dev libjpeg8-dev
    libkrb5-dev libmysqlclient-dev libncurses5-dev libpcre3-dev libperl-dev libpng12-dev librtmp-dev libsane-dev libsnmp-dev libsqlite3-dev libssl-dev libstdc++6-4.4-dev libstdc++6-4.7-dev libtiff5-dev libtool libusb++-dev libusb-1.0-0-dev libusb-dev linux-generic
    linux-headers-3.5.0-23 linux-headers-3.5.0-23-generic linux-headers-3.5.0-25 linux-headers-3.5.0-25-generic linux-headers-generic linux-image-3.5.0-17-generic linux-image-3.5.0-18-generic linux-image-3.5.0-19-generic linux-image-3.5.0-21-generic
    linux-image-3.5.0-22-generic linux-image-3.5.0-23-generic linux-image-3.5.0-25-generic linux-image-extra-3.5.0-17-generic linux-image-extra-3.5.0-18-generic linux-image-extra-3.5.0-19-generic linux-image-extra-3.5.0-21-generic linux-image-extra-3.5.0-22-generic
    linux-image-extra-3.5.0-23-generic linux-image-extra-3.5.0-25-generic linux-image-generic linux-libc-dev lsb-core python-dev python2.7-dev ruby-dev ruby1.8-dev ruby1.9.1-dev zlib1g-dev
    0 upgraded, 0 newly installed, 66 to remove and 0 not upgraded.
    Remv build-essential [11.5ubuntu3]
    Remv libcurl4-nss-dev [7.27.0-1ubuntu1.1]
    Remv libcupsimage2-dev [1.6.1-0ubuntu11.3]
    Remv libcups2-dev [1.6.1-0ubuntu11.3]
    Remv libkrb5-dev [1.10.1+dfsg-2]
    Remv krb5-multidev [1.10.1+dfsg-2]
    Remv comerr-dev [2.1-1.42.5-1ubuntu2]
    Remv g++ [4:4.7.2-1ubuntu2]
    Remv libusb++-dev [2:0.1.12-23]
    Remv g++-4.4 [4.4.7-2ubuntu1] [libstdc++6-4.4-dev:amd64 ]
    Remv libstdc++6-4.4-dev [4.4.7-2ubuntu1]
    Remv g++-4.7 [4.7.2-2ubuntu1] [libstdc++6-4.7-dev:amd64 ]
    Remv libstdc++6-4.7-dev [4.7.2-2ubuntu1]
    Remv google-earth-stable [6.0.3.2197-r0]
    Remv ruby-dev [4.9]
    Remv ruby1.9.1-dev [1.9.3.194-1ubuntu1.3]
    Remv ruby1.8-dev [1.8.7.358-4ubuntu0.1]
    Remv lsb-core [4.0-0ubuntu26.1]
    Remv libsane-dev [1.0.23-0ubuntu1]
    Remv libtiff5-dev [4.0.2-1ubuntu2.1]
    Remv libsnmp-dev [5.4.3~dfsg-2.5ubuntu1]
    Remv libperl-dev [5.14.2-13ubuntu0.1]
    Remv librtmp-dev [2.4+20111222.git4e06e21-1]
    Remv libgnutls-dev [2.12.14-5ubuntu4.1]
    Remv python-dev [2.7.3-0ubuntu7]
    Remv python2.7-dev [2.7.3-5ubuntu4]
    Remv libssl-dev [1.0.1c-3ubuntu2.1]
    Remv libmysqlclient-dev [5.5.29-0ubuntu0.12.10.1]
    Remv libpng12-dev [1.2.49-1ubuntu1]
    Remv zlib1g-dev [1:1.2.7.dfsg-13]
    Remv libgphoto2-2-dev [2.4.14-2]
    Remv libusb-dev [2:0.1.12-23]
    Remv libusb-1.0-0-dev [2:1.0.12-2]
    Remv libtool [2.4.2-1ubuntu2]
    Remv libsqlite3-dev [3.7.13-1]
    Remv libpcre3-dev [1:8.30-5ubuntu1]
    Remv libgcrypt11-dev [1.5.0-3ubuntu1]
    Remv libexpat1-dev [2.1.0-1ubuntu1]
    Remv libexif-dev [0.6.20-3]
    Remv libncurses5-dev [5.9-10ubuntu1]
    Remv libjpeg-dev [8c-2ubuntu7]
    Remv libjpeg8-dev [8c-2ubuntu7]
    Remv libjpeg-turbo8-dev [1.2.1-0ubuntu2]
    Remv libc6-dev [2.15-0ubuntu20]
    Remv linux-generic [3.5.0.25.31]
    Remv linux-headers-3.5.0-23-generic [3.5.0-23.35]
    Remv linux-headers-3.5.0-23 [3.5.0-23.35]
    Remv linux-headers-generic [3.5.0.25.31]
    Remv linux-headers-3.5.0-25-generic [3.5.0-25.38]
    Remv linux-headers-3.5.0-25 [3.5.0-25.38]
    Remv linux-image-extra-3.5.0-17-generic [3.5.0-17.28]
    Remv linux-image-3.5.0-17-generic [3.5.0-17.28]
    Remv linux-image-extra-3.5.0-18-generic [3.5.0-18.29]
    Remv linux-image-3.5.0-18-generic [3.5.0-18.29]
    Remv linux-image-extra-3.5.0-19-generic [3.5.0-19.30]
    Remv linux-image-3.5.0-19-generic [3.5.0-19.30]
    Remv linux-image-extra-3.5.0-21-generic [3.5.0-21.32]
    Remv linux-image-3.5.0-21-generic [3.5.0-21.32]
    Remv linux-image-extra-3.5.0-22-generic [3.5.0-22.34]
    Remv linux-image-3.5.0-22-generic [3.5.0-22.34]
    Remv linux-image-extra-3.5.0-23-generic [3.5.0-23.35]
    Remv linux-image-3.5.0-23-generic [3.5.0-23.35]
    Remv linux-image-generic [3.5.0.25.31]
    Remv linux-image-extra-3.5.0-25-generic [3.5.0-25.38]
    Remv linux-image-3.5.0-25-generic [3.5.0-25.38]
    Remv linux-libc-dev [3.5.0-25.38]

    Reply
    • Linerd

      What listing do you get if you just enter to portion prior to the xargs command?

      dpkg -l linux-* | awk '/^ii/{ print $2}' | grep -v -e `uname -r | cut -f1,2 -d"-"` | grep -e [0-9]

      I've also documented a more manual way if you want to try that: http://tuxtweaks.com/2009/12/remove-old-kernels-in-ubuntu/

      Reply
    • Matt

      I saw the same behavior and the problem is caused by the naming of the linux-libc-dev package. I fixed the problem by changing the command dpkg -l linux-* to dpkg -l linux-headers* -l linux-image*.

      The full command would be dpkg -l linux-headers* -l linux-image* | awk '/^ii/{ print $2}' | grep -v -e `uname -r | cut -f1,2 -d"-"` | grep -e [0-9] | xargs sudo apt-get remove

      Reply

  22. Today, after 24 months of having old kernels pile up in /sda3 to the point that the OS failed, this post has removed the outdated kernels, and I have recovered 3 gig. Thank you very very much.

    Reply
  23. Nate

    Thanks for not only sharing the command, but the logic behind it.

    Reply
  24. Patrick

    Hi Guys
    I get this error when using this
    E: Unmet dependencies. Try 'apt-get -f install' with no packages (or specify a solution).

    This may be because it failed to install correctly as the /boot is 100% full can you repost the solution showing where you would put just the one image to remove in order to clean space so i can run it again please
    Many thanks

    Reply
    • Linerd

      This post shows a more manual way of removing old kernels, if that's what you're looking for:
      http://tuxtweaks.com/2009/12/remove-old-kernels-in-ubuntu/

      Reply
      • Patrick

        Hi Linerd
        That worked a treat thanks. My problem was an incomplete linux header update to 3.2.0-34 due to not enough space and everything i did failed because of this. With the old solution i was able to remove older versions one at a time and create the space to complete the update and remove all of the old ones with your in one command

        Many thanks it was very much appreciated

        Reply
  25. a2461895

    Beware of what you're doing if using this.
    This method is not fool-proof.
    On my system it wants to remove linux-libc-dev:amd64 too, which is a bad idea.

    Yes, it works on on 32-bits systems as the author already wrote, but I'm not so sure about 64-bits systems. There might be other packages that also meet the requirements and that are removed unexpectedly.

    Reply
  26. Ed Holcroft

    This worked great on all my Ubuntu 12.04 64-bit servers on AWS. Thanks.

    Reply

  27. Works great in Ubuntu 12.04.1 LTS 32 bits as well. Thanks for adding a step wise tutorial, with details about each step. Great way to learn things. Kudos.

    Reply
  28. Bruno

    Works great, Ubuntu 12.04 LTS 64 bits. Thanks!

    Reply

  29. I just had to cleanup my netbook and added a blog with more background info and a simpler command on my page; I'll keep that up to date when I find a newer or better way:

    http://marc.waeckerlin.org/computer/blog/remove_all_old_kernel_images_in_ubuntu

    Reply
  30. John M

    Thanks very much. I needed this to remotely clear space from /boot on a remote PC using ssh. It worked a treat!

    Reply
  31. Sriram Rangan

    Amazing, I was really sleepy and this was just what I was looking for. Good job mate.

    Reply

  32. Seems to work in the 64 bit PAE version as well! Thanks for the awesomeness!

    Reply
  33. Gniourf

    With bash (don't know/care about other shells), it's better to quote the * in the dpkg command, to avoid an unwanted glob, just in case a file linux-something exists:
    dpkg -l linux-'*'

    Reply

  34. There's a simpler way: Remove all but the bare necessary: You can add a plus (+) behind the packages you want to keep:

    First get your kernel type:

    ktype=$(uname -r | sed 's/.*-\(generic\|i386\|server\|common\|rt\|xen\|ec2\)/\1/')

    If you already know your kernel typ, you may set it directly, e.g.:

    ktype=generic

    Then use the variable "ktype" to remove everything but the actual kernel image:


    sudo apt-get remove --purge 'linux-(headers|image)-.*' linux-image-$(uname -r)+ linux-image-$ktype+ linux-$ktype+

    If you need the headers, keep them too:


    sudo apt-get remove --purge 'linux-(headers|image)-.*' linux-headers-$(uname -r)+ linux-headers-$(uname -r | sed s/-$ktype//)+ linux-headers-$ktype+ linux-image-$(uname -r)+ linux-image-$ktype+ linux-$ktype+

    Reply
    • Linerd

      My current kernel = 3.2.0-29-generic

      Using a dry run with your first command gives on my system

      Remv linux-headers-3.2.0-23-generic [3.2.0-23.36]
      Remv linux-headers-3.2.0-23 [3.2.0-23.36]
      Remv linux-headers-3.2.0-24-generic [3.2.0-24.39]
      Remv linux-headers-3.2.0-24 [3.2.0-24.39]
      Remv linux-headers-3.2.0-25-generic [3.2.0-25.40]
      Remv linux-headers-3.2.0-25 [3.2.0-25.40]
      Remv linux-headers-3.2.0-26-generic [3.2.0-26.41]
      Remv linux-headers-3.2.0-26 [3.2.0-26.41]
      Remv linux-headers-3.2.0-27-generic [3.2.0-27.43]
      Remv linux-headers-3.2.0-27 [3.2.0-27.43]
      Remv nvidia-current [295.40-0ubuntu1.1]
      Remv linux-headers-generic [3.2.0.29.31]
      Remv linux-headers-3.2.0-29-generic [3.2.0-29.46]
      Remv linux-headers-3.2.0-29 [3.2.0-29.46]
      Remv linux-image-3.2.0-23-generic [3.2.0-23.36]
      Remv linux-image-3.2.0-24-generic [3.2.0-24.39]
      Remv linux-image-3.2.0-25-generic [3.2.0-25.40]
      Remv linux-image-3.2.0-26-generic [3.2.0-26.41]
      Remv linux-image-3.2.0-27-generic [3.2.0-27.43]

      So I would lose my current kernel headers and for some reason, my nvidia driver. I don't want to lose my nvidia driver.

      Whereas a dry run with my command gives on my system

      Remv linux-headers-3.2.0-23-generic [3.2.0-23.36]
      Remv linux-headers-3.2.0-23 [3.2.0-23.36]
      Remv linux-headers-3.2.0-24-generic [3.2.0-24.39]
      Remv linux-headers-3.2.0-24 [3.2.0-24.39]
      Remv linux-headers-3.2.0-25-generic [3.2.0-25.40]
      Remv linux-headers-3.2.0-25 [3.2.0-25.40]
      Remv linux-headers-3.2.0-26-generic [3.2.0-26.41]
      Remv linux-headers-3.2.0-26 [3.2.0-26.41]
      Remv linux-headers-3.2.0-27-generic [3.2.0-27.43]
      Remv linux-headers-3.2.0-27 [3.2.0-27.43]
      Remv linux-image-3.2.0-23-generic [3.2.0-23.36]
      Remv linux-image-3.2.0-24-generic [3.2.0-24.39]
      Remv linux-image-3.2.0-25-generic [3.2.0-25.40]
      Remv linux-image-3.2.0-26-generic [3.2.0-26.41]
      Remv linux-image-3.2.0-27-generic [3.2.0-27.43]

      Your final command gives the same result as mine.

      Does the "+" notation tell apt-get to install the package rather than remove it? I'm not familiar with that option.

      Reply
      • Linerd

        OK, I found it in the man page:

        If a plus sign is appended to the package name (with no intervening space), the identified package will be installed instead of removed.

        Reply
  35. martini

    Why not just

    $sudo apt-get autoremove

    ?

    Reply
    • Linerd

      Does

      sudo apt-get autoremove

      remove old kernels for you? It doesn't on any of my systems.

      Reply

  36. I use this script to clear my kernels


    #/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
    aptitude remove $I
    done
    rm -f /tmp/kernelList
    update-grub

    Reply
  37. bettyfurness

    superboer12 - great command, worked fine on my 64 bit Lucid. How would I modify this script to keep the last two kernels? I had .39, .40 and .41. After running this I had only .41. Booted OK but I always worry.

    Thanks!

    Reply
  38. fintux

    Thanks, this made my life alot easier! :)

    Reply
  39. EriK

    Thanks worked a treat!

    Reply

  40. Hi all ... thank you for the nice command ... I have just run it on my Ubuntu Lucid 64 bit and it worked perfectly ... :-)

    Reply

  41. i'v been updating for almost 2 years now.. this freed 2.7 GB of storage.. THANKS

    Reply
  42. Ubuntu Server

    Worked perfectly for me.
    After I finished, I ran /usr/sbin/update-grub just to be safe.

    Matt Locke - Nashville, TN

    Reply
  43. superboer12

    I modified it a little bit to keep both the running and the newest kernel.
    Thanks for the script!
    [code]
    rmkernel () {
    local cur_kernel=$(uname -r|sed 's/-*[a-z]//g'|sed 's/-386//g')
    local kernel_pkg="linux-(image|headers|ubuntu-modules|restricted-modules)"
    local meta_pkg="${kernel_pkg}-(generic|i386|server|common|rt|xen|ec2|amd64)"
    local pre_kernel=$(echo ${cur_kernel} | tr '' \\\n | awk 'BEGIN{FS="-";OFS="-"} {print $1,$2-1}')
    local latest_kernel=$(dpkg -l linux-* | awk '/^ii/{ print $2}' | grep -v -e `uname -r | cut -f1,2 -d"-"` | grep -e [0-9] | tail -1 | cut -f3,4 -d"-")
    aptitude purge -y $(dpkg -l | egrep $kernel_pkg | egrep -v "${cur_kernel}|${meta_pkg}|${latest_kernel}" | awk '{print $2}')
    }

    rmkernel

    exit 0
    [/code]

    Reply
    • superboer12

      Little correction:


      #!/bin/bash

      rmkernel () {
      local cur_kernel=$(uname -r|sed 's/-*[a-z]//g'|sed 's/-386//g')
      local kernel_pkg="linux-(image|headers|ubuntu-modules|restricted-modules)"
      local meta_pkg="${kernel_pkg}-(generic|i386|virtual|server|common|rt|xen|ec2|amd64)"
      local latest_kernel=$(dpkg -l linux-* | awk '/^ii/{ print $2}' | grep -e [0-9] | tail -1 | cut -f3,4 -d"-")
      aptitude purge -y $(dpkg -l | egrep $kernel_pkg | egrep -v "${cur_kernel}|${meta_pkg}|${latest_kernel}" | awk '{print $2}')
      }

      rmkernel

      exit 0

      Reply
  44. Cesare

    Thank you!!! It worked like a charm.

    Reply
  45. Warfie

    That's rather more than one command. LOL

    Reply
    • Linerd

      Yes, I suppose that's true, but it is all on one line! ;)

      Reply
  46. R E Broadley

    Very nice, but this article seems to assume that you're already running the OS with the latest kernel, so as this article goes, it will delete all kernels except the one currently running, which may be an old one.

    What would be a better command is if it deleted all kernels except the latest one (and perhaps also the one currently running).

    Speaking of which, why doesn't ubuntu default to running the latest one? Despite installing kernel updates, mine's still defaulting to an old one...

    Reply
    • Jona

      That's a configuration option. You can adjust the booted kernel in /etc/default/grub

      Reply
  47. Santanu

    It's always a good idea to keep the last kernel installed. So, I'd modify the rmkernel script to add this line for the previous version of kernel:
    [code]local pre_kernel=$(echo ${cur_kernel} | tr '' \\\n | awk 'BEGIN{FS="-";OFS="-"} {print $1,$2-1}')[/code]
    and then add pre_kerne to the exclude list:
    [code]sudo aptitude purge $(dpkg -l | egrep $kernel_pkg | egrep -v "${cur_kernel}|${pre_kernel}|${meta_pkg}" | awk '{print $2}'[/code]

    For the one-liner:
    [code]dpkg -l linux-* | awk '/^ii/{ print $2}' | grep -v -e `uname -r | cut -f1,2 -d"-"` -e `uname -r | gawk 'BEGIN{FS="-";OFS="-"} {print $1,$2-1}'` | grep -e [0-9][/code]

    Cheers!!

    Reply
  48. K

    Thanks for the handy script.
    I had to add -y argument to apt-get to make it work. Ubuntu 10.10.

    Reply
  49. DKlann

    To take it one step further, you can purge all the old configuration files with the following (after completing the Big One Liner (above):

    dpkg -l 'linux-*' | awk '/^rc/{ print $2}' | sudo xargs apt-get -y purge

    Thanks for this nice little ditty!

    Does anyone know how (or whether it makes sense) to delay the execution of the postrm.d scripts until after all the pieces are removed? It gets a little tiresome watching all those initramfs and zz-update-grub calls...

    Reply
    • Linerd

      Thanks for the tip. I didn't know those rc status config files were still hanging around. However, when I try your command I get this type of error:

      Package linux-image-2.6.32-27-generic is not installed, so not removed

      When I modify your command to:

      dpkg -l 'linux-*' | awk '/^rc/{ print $2}' | sudo xargs dpkg --purge

      it works.

      After doing some research, it looks like if I change my one-liner to purge instead of remove, then it should get rid of the config files as well and make this all moot, so I'm updating my post. I'll install an older kernel and re-run the command to see if it works.

      Reply
    • Linerd

      OK, I verified that changing my one-liner to purge instead of remove resolves the config file issue.

      Reply
  50. David

    Thanks it worked great.

    Reply

  51. it works with 64-bit. thanks

    Reply
  52. Vern

    "I'd be interested to know if it works just as well on a 64 bit system"
    Testing on Ubuntu Lucid amd64 system, and it works.

    Also someone posted this at Ubuntu forums a while back as a function script that works well:

    rmkernel () {
            local cur_kernel=$(uname -r|sed 's/-*[a-z]//g'|sed 's/-386//g')
            local kernel_pkg="linux-(image|headers|ubuntu-modules|restricted-modules)"
            local meta_pkg="${kernel_pkg}-(generic|i386|server|common|rt|xen|ec2)"
            sudo aptitude purge $(dpkg -l | egrep $kernel_pkg | egrep -v "${cur_kernel}|${meta_pkg}" | awk '{print $2}')
    }
    
    rmkernel
    
    exit 0
    
    Reply
    • Linerd

      Thanks for the 64 bit verification and for the nice tip about the script.

      Reply

    • Thanks for posting my rmkernel script (fun to see where it ends up!)

      I made a new version, which can also be used for Debian, as the previous version did not work for that OS.

      http://blog.opperschaap.net/2011/02/04/removing-old-kernels-in-debian-or-ubuntu/

      Reply
    • Santanu

      It's always a good idea to keep the previous kernel installed, in case you need to roll back. So, I'd add another line for previous kernel:

      local pre_kernel=$(echo ${cur_kernel} | tr '' \\\n | awk 'BEGIN{FS="-";OFS="-"} {print $1,$2-1}')

      and then modify the 'aptitude' to add 'pre_kernel' to the exclude list:

      sudo aptitude purge $(dpkg -l | egrep $kernel_pkg | egrep -v "${cur_kernel}|${pre_kerne}|${meta_pkg}" | awk '{print $2}')

      Cheers!!

      Reply
  53. carl801

    Hey, it works. Had to run update-grub, then because I'm using BURG, had to run update-burg. There oughta be a button somewhere for this command, eh?

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>