2008年8月20日星期三

ubuntu删除旧内核的shell脚本

随着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

没有评论: