Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # this is a script for purging unused old kernels installed in your system,
- # you should use this script under newest kernel installed!!!
- # Please, run it with sudo or as a root user (needed only for apt-get purge operation)!
- # checking for root privileges
- if [ "$(id -u)" != "0" ]; then
- echo 'This script must be run as root or with sudo command!!!' 2>&1
- exit 1
- fi
- # let's get installed and removed kernels list
- # and exclude removed kernels
- # and exclude kernel you are using now
- # excluding kernel metapackage too (list only linux-image-2....),
- # then we are going to write this list to a temporary file
- dpkg --get-selections linux-image*\
- | grep -v deinstall\
- | grep -v `uname -r`\
- | grep 2 > /tmp/tmp.txt
- # asking user if he wants to purge kernels found...
- echo "Old kernels found:"
- cat /tmp/tmp.txt
- echo "You are now running kernel:"
- echo linux-image-`uname -r`
- CORRECT_INPUT=0
- while [ $CORRECT_INPUT -ne 1 ]
- do
- echo -n "Would you like to purge old kernels found? (y/n): "
- read CHOISE
- case $CHOISE in
- y)
- echo "Purging kernels!..."
- CORRECT_INPUT=1
- ;;
- n)
- rm /tmp/tmp.txt
- echo "Exiting..."
- CORRECT_INPUT=1
- exit 0
- ;;
- *)
- echo "Please enter y or n and press enter!"
- ;;
- esac
- done
- # let's purge unused kernels which are in our tempfile
- apt-get -s purge `cat /tmp/tmp.txt | cut --fields=1 -`
- echo -n 'Done! Press any key to continue!'
- read CONTINUE
- # finding old kernel-headers
- dpkg --get-selections linux-headers*\
- | grep -v deinstall\
- | grep 2 > /tmp/tmp.txt
- # asking user if he wants to remove old kernel haders too...
- echo "Old kernel headers found, script can purge them too:"
- cat /tmp/tmp.txt
- echo "Your current kernel headers version:"
- echo linux-headers-`uname -r`
- CORRECT_INPUT2=0
- while [ $CORRECT_INPUT2 -ne 1 ]
- do
- echo -n "Would you like to purge old kernels headers? (y/n): "
- read CHOISE
- case $CHOISE in
- y)
- echo "Purging kernel headers!..."
- CORRECT_INPUT2=1
- ;;
- n)
- rm /tmp/tmp.txt
- echo "Exiting..."
- CORRECT_INPUT2=1
- exit 0
- ;;
- *)
- echo "Please enter y or n and press enter!"
- ;;
- esac
- done
- # purging kernel-headers which are in our tempfile
- apt-get -s purge `cat /tmp/tmp.txt | cut --fields=1 -`
- rm /tmp/tmp.txt
- echo 'All done! Exiting script...'
- exit 0
Advertisement
Add Comment
Please, Sign In to add comment