Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: Script to remove old kernels

  1. #1
    Join Date
    Jul 2008
    Beans
    87

    Lightbulb Script to remove old kernels

    Originally posted here:http://www.go2linux.org/clean-linux-...ages-grub-menu by Deven
    Just too damn good not to share. Especially seeing as how when I attempted to manually remove the old kernels I ended up spending a few hours getting a kernel back on after deleting all of them.

    Code:
    #/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 #not sure if needed
    save as cleanKernels.sh
    Execute as root or chmod 755 it.
    Code:
    sudo bash cleanKernels.sh
    Yippee you just freed up some disk space!

  2. #2

    Re: Script to remove old kernels

    Very cool. Is there any easy way to modify it so that it also retains your next most recent kernel (in addition to your current one) as backup, and only removes kernels from the third one onwards?

  3. #3
    Join Date
    Nov 2009
    Location
    Croatia/Hrvatska
    Beans
    492
    Distro
    Ubuntu 12.10 Quantal Quetzal

    Re: Script to remove old kernels

    Code:
    sudo apt-get install grub-customizer
    you can edit grub menu, change default os, change background picture etc...

  4. #4
    Join Date
    Jul 2008
    Beans
    87

    Re: Script to remove old kernels

    I believe this will get the job done for you.
    It is UNTESTED since I cleaned out my extra kernels but I'm pretty sure that it works keeping the last entry in the /tmp/kernelList file. This last entry should be the latest due to alphanumeric reporting of ls.
    Code:
    #!/bin/bash
    counter=1
    ls /boot/ | grep vmlinuz | sed 's@vmlinuz-@linux-image-@g' | grep -v `uname -r ` > /tmp/kernelList
    numberOfExtraKernels=`wc -l /tmp/kernelList`
    for l in `cat /tmp/kernelList`
    do
        if [ $counter -lt $numberOfExtraKernels ]
        then 
            aptitude remove $l
            let "counter +=1"
        fi
    done
    rm -f /tmp/kernelList
    updage-grub

  5. #5
    Join Date
    Jun 2009
    Location
    SW Forida
    Beans
    Hidden!
    Distro
    Kubuntu

    Re: Script to remove old kernels

    Have not tried it. but minor typo.

    updage-grub
    s/b
    update-grub
    UEFI boot install & repair info - Regularly Updated :
    https://ubuntuforums.org/showthread.php?t=2147295
    Please use Thread Tools above first post to change to [Solved] when/if answered completely.

  6. #6
    Join Date
    Jun 2009
    Location
    SW Forida
    Beans
    Hidden!
    Distro
    Kubuntu

    Re: Script to remove old kernels

    I do not know bash so I had to do a little research.

    This worked for me, I had to add the echo to know what was what and remove the file name from the wc command.

    Code:
    #!/bin/bash
    counter=2
    ls /boot/ | grep vmlinuz | sed 's@vmlinuz-@linux-image-@g' | grep -v `uname -r ` > /tmp/kernelList
    numberOfExtraKernels=`wc -l /tmp/kernelList | awk '{print $1}'`
    for l in `cat /tmp/kernelList`
    do
        if [ "$counter" -lt "$numberOfExtraKernels" ]
        echo "$l", "$counter", "$numberOfExtraKernels"
        then 
            aptitude remove $l
            let "counter +=1"
        fi
    done
    rm -f /tmp/kernelList
    update-grub
    When I ran it with counter =1 it deleted all but 1, so now I cannot test with counter =2.
    UEFI boot install & repair info - Regularly Updated :
    https://ubuntuforums.org/showthread.php?t=2147295
    Please use Thread Tools above first post to change to [Solved] when/if answered completely.

  7. #7
    Join Date
    Jan 2010
    Location
    Southaven, MS
    Beans
    187
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Script to remove old kernels

    One liner below to remove all but the latest kernel:

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

  8. #8
    Join Date
    Feb 2010
    Beans
    15

    Re: Script to remove old kernels

    Quote Originally Posted by marin123 View Post
    Code:
    sudo apt-get install grub-customizer
    you can edit grub menu, change default os, change background picture etc...
    No such package in the normal Ubuntu repos. What PPA did you get it from?

  9. #9
    Join Date
    Nov 2009
    Location
    Croatia/Hrvatska
    Beans
    492
    Distro
    Ubuntu 12.10 Quantal Quetzal

    Re: Script to remove old kernels

    Quote Originally Posted by HunterZ View Post
    No such package in the normal Ubuntu repos. What PPA did you get it from?
    https://launchpad.net/~danielrichter...rub-customizer

    you have instructions on this link...

  10. #10
    Join Date
    Jul 2007
    Beans
    1

    Re: Script to remove old kernels

    Code:
    #! /bin/sh
    
    oldKernelsList=$(ls /boot/ | grep vmlinuz  | grep -v `uname -r` | sed 's/vmlinuz-\(.*\)\(-.*\)$/linux-image-\1\2\nlinux-tools-\1\nlinux-headers-\1\2\nlinux-headers-\1\n/g') && aptitude remove ${oldKernelsList} && update-grub
    bye

Page 1 of 2 12 LastLast

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •