Chinaunix首页 | 论坛 | 博客
  • 博客访问: 967319
  • 博文数量: 113
  • 博客积分: 7235
  • 博客等级: 少将
  • 技术积分: 2101
  • 用 户 组: 普通用户
  • 注册时间: 2008-07-14 11:24
文章分类

全部博文(113)

文章存档

2013年(7)

2012年(5)

2011年(6)

2010年(8)

2009年(15)

2008年(72)

分类: LINUX

2008-08-07 10:52:27

随着ubuntu的更新,特别是其内核的更新,系统中就会存留很多版本的内核,要知道每个都很大,不但浪费硬盘空间,而且在启动的时候,grub的选项里会出现很多个内核,很是不好,所以就需要删除旧版本的已经不用的内核。一下的shell脚本就可以帮你轻松搞定问题,这个脚本是转载学长。

#!/bin/sh
#Program:
# Let user uninstall unused kernels which installed as debian package form.
#Author:
# mtyy110
if [ "`whoami`" != 'root' ]; then
        echo 'Requires superuser privilege.'
        exit 1
fi
dpkg --get-selections | grep 'linux-' | grep -v 'deinstall' | grep "\-[0-9]\.[0-9]\{1,2\}\.[0-9]\{1,2\}\-"
while [ 1 ]
do
        total=`dpkg --get-selections | grep 'linux-' | grep -v 'deinstall' | grep "\-[0-9]\.[0-9]\{1,2\}\.[0-9]\{1,2\}\-" | wc -l`
        read -p "Which version would you like to uninstall?(0 to quit)" version
        if [ $version = "0" ]; then
                break
        fi
        tmp=`echo $version | grep "^[0-9]\.[0-9]\{1,2\}\.[0-9]\{1,2\}\-\{0,1\}[0-9]\{0,2\}$" | wc -l`
        if [ $tmp -eq 0 ]; then
                echo "Not an available version format,please input full version."
                continue
        fi
        sum=`dpkg --get-selections | grep 'linux-' | grep -v 'deinstall' | grep "\-[0-9]\.[0-9]\{1,2\}\.[0-9]\{1,2\}\-" | grep "$version" | wc -l`
        if [ $sum -eq 0 ]; then
                echo "Not find version $version.Ignored."
                continue
        fi
        tmp=`uname -r | grep "$version" | wc -l`
        if [ $tmp -eq 1 ]; then
                read -p "This will uninstall the kernel current used:`uname -r`.Are you sure?(y/N)" choice
                if [ "$choice" != 'y' -a "$choice" != 'Y' ]; then
                        continue
                fi
        fi
        if [ $total -le $sum ]; then
                read -p "This will uninstall all the kernels in the system.Are you sure?(y/N)" choice
                if [ "$choice" -o 'y' -a "$choice" -o 'Y' ]; then
                        continue
                fi
        fi
        apt-get remove `dpkg --get-selections | grep 'linux-' | grep -v 'deinstall' | grep "\-$version" | cut -f 1`
        read -p "Continue to uninstall other kernel?(Y/n)" choice
        if [ "$choice" = 'n' -o "$choice" = 'N' ]; then
                break
        fi
        dpkg --get-selections | grep 'linux-' | grep -v 'deinstall' | grep "\-[0-9]\.[0-9]\{1,2\}\.[0-9]\{1,2\}\-"
done
exit 0

阅读(1367) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~