清理ubuntu老内核的小脚本

上次有文章关于清理系统旧内核:

http://www.linusing.com/how-to-clean-up-the-old-kernel-ubuntu-linux-system.html/

http://www.linusing.com/ubuntu-updated-the-kernel-and-cleaning-up-unused-files.html/

今天在http://li2z.cn/2010/02/28/clean_ubuntu_kernel/

看到 lazy bone 写的旧内核清理脚本,非常强大!和大家分享!

ubuntu用久了,升级了几次内核以后,就免不了需要清理一下老的内核,毕竟一套内核就有170多M的空间呢。但是每次都打一堆字来清理也烦了。虽然Ubuntu 命令技巧(这里再罗嗦几句:如果还有ubuntuer没看过这个,强烈建议看一遍。)里有删除旧内核的命令,而且就一行:

sudo aptitude purge ~ilinux-image-.*\(\!`uname -r`\)

但是我却不是很喜欢,因为这这个没有把linux-headers-xxx删除掉。
于是自己写了个繁的,需要的可以自己保存,以便不时之需。

#!/bin/sh
# 清理ubuntu的老内核
# by bones7456
# http://li2z.cn
CURRENT="`uname -r | awk -F"-" '{print $1"-"$2}'`"
HEADERS=""
IMAGES=""
for HEADER in `dpkg --get-selections | grep ^linux-headers | \
grep -vE "(generic|386|virtual)" | awk '{gsub(/linux-headers-/,"",$1);print $1}'`
do
    if [[ "$CURRENT" < "$HEADER" ]]
    then
        echo "正在运行的内核不是最新的。 $CURRENT < $HEADER"
        echo "Running kernel is not the newest. $CURRENT < $HEADER"
        exit 1
    else
        [[ "$CURRENT" != "$HEADER" ]] && {
            HEADERS="${HEADERS} linux-headers-${HEADER}"
            IMAGE="`dpkg --get-selections | grep ^linux-image | \
                grep "${HEADER}" | awk '{print $1}'`"
            IMAGES="${IMAGES} $IMAGE"
        }
    fi
done

if [[ x"$HEADERS" == x"" ]]
then
    echo "没有要清理的老内核."
    echo "No old kernel need to clean."
    exit 0
fi
CMD="sudo apt-get purge $HEADERS $IMAGES"
echo "$CMD"
if [ "$1" == "-e" ]
then
    sh -c "$CMD"
else
    echo "请确定以上命令是否正确,然后输入 $0 -e 来执行以上命令。"
    echo "Be sure this command is right, then type $0 -e to execute."
fi

Popularity: 1% [?]

Related posts:

  1. 怎样清理Ubuntu Linux 系统旧内核
  2. ubuntu 更新内核与清理无用文件
  3. 安装/卸载deb包文件
  4. Apt-get/dpkg简明入门
  5. Ubuntu 编译一个内核
Share this Post:
Digg Google Bookmarks reddit Mixx StumbleUpon Technorati Yahoo! Buzz DesignFloat Delicious BlinkList Furl

No Responses to “清理ubuntu老内核的小脚本”

Leave a Reply:

Name (required):
Mail (will not be published) (required):
Website:
Comment (required):
XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>