一键清除Ubuntu旧内核 old kernel 22年亲测可用

0.简介与功能

一个脚本一键运行,删除旧内核同时保留当前kernel。

核心代码如下,可以复制直接运行

 sudo apt-get purge $(dpkg-query -W -f'${Package}\n' 'linux-*' |      
sed -nr 's/.*-([0-9]+(\.[0-9]+){2}-[^-]+).*/\1 &/p' | linux-version sort |
awk '($1==c){exit} {print $2}' c=$(uname -r | cut -f1,2 -d-))

1准备工作,可跳过

1.1 查看当前kernel

uname -r

在这里插入图片描述

1.2 查看当前kernel list

 dpkg --list 'linux-image-*'

一般会有很多旧的内核,下图因为清理过所以没有无用的内核。
在这里插入图片描述

2.清理

以下即为核心代码!!!!!!!!!!

 sudo apt-get purge $(dpkg-query -W -f'${Package}\n' 'linux-*' |      
sed -nr 's/.*-([0-9]+(\.[0-9]+){2}-[^-]+).*/\1 &/p' | linux-version sort |
awk '($1==c){exit} {print $2}' c=$(uname -r | cut -f1,2 -d-))

附录:代码解释,可以不看。有兴趣可以看看细节

代码解释

Ref: https://askubuntu.com/questions/401581/bash-one-liner-to-delete-only-old-kernels/620515#620515

1.

dpkg -l linux-{image,headers}-* | awk ‘/^ii/{print $2}’ Just like Malte suggested. Lists the relevant kernel files.

2.

egrep ‘[0-9]+.[0-9]+.[0-9]+’ Also suggested by Malte as the safer way to pick out only the kernel files by looking for a version number.

3.

Since we now are possibly listing both the image and the header packages, the package naming can vary so we have this awk workaround which is necessary for the sort awk ‘BEGIN{FS=“-”}; {if ($3 ~ /[0-9]+/) print $3"-“$4,$0; else if ($4 ~ /[0-9]+/) print $4”-"$5,$0}’ The result is a new column with the version number before the original package name like below:

$ dpkg -l linux-{image,headers}-* | awk '/^ii/{print $2}' | egrep '[0-9]+\.[0-9]+\.[0-9]+' | awk 'BEGIN{FS="-"}; {if ($3 ~ /[0-9]+/) print $3"-"$4,$0; else if ($4 ~ /[0-9]+/) print $4"-"$5,$0}'
3.11.0-23 linux-headers-3.11.0-23
3.11.0-23 linux-headers-3.11.0-23-generic
3.11.0-24 linux-headers-3.11.0-24
3.11.0-24 linux-headers-3.11.0-24-generic
3.11.0-26 linux-headers-3.11.0-26
3.11.0-26 linux-headers-3.11.0-26-generic
3.11.0-23 linux-image-3.11.0-23-generic
3.11.0-24 linux-image-3.11.0-24-generic
3.11.0-26 linux-image-3.11.0-26-generic
3.8.0-35 linux-image-3.8.0-35-generic
3.11.0-23 linux-image-extra-3.11.0-23-generic
3.11.0-24 linux-image-extra-3.11.0-24-generic
3.11.0-26 linux-image-extra-3.11.0-26-generic
3.8.0-35 linux-image-extra-3.8.0-35-generic

4.

Now we must sort the list in order to prevent uninstalling any newer images than the one that is currently running. sort -k1,1 --version-sort -r giving us this:

$ dpkg -l linux-{image,headers}-* | awk '/^ii/{print $2}' | egrep '[0-9]+\.[0-9]+\.[0-9]+' | awk 'BEGIN{FS="-"}; {if ($3 ~ /[0-9]+/) print $3"-"$4,$0; else if ($4 ~ /[0-9]+/) print $4"-"$5,$0}' | sort -k1,1 --version-sort -r
3.11.0-26 linux-image-extra-3.11.0-26-generic
3.11.0-26 linux-image-3.11.0-26-generic
3.11.0-26 linux-headers-3.11.0-26-generic
3.11.0-26 linux-headers-3.11.0-26
3.11.0-24 linux-image-extra-3.11.0-24-generic
3.11.0-24 linux-image-3.11.0-24-generic
3.11.0-24 linux-headers-3.11.0-24-generic
3.11.0-24 linux-headers-3.11.0-24
3.11.0-23 linux-image-extra-3.11.0-23-generic
3.11.0-23 linux-image-3.11.0-23-generic
3.11.0-23 linux-headers-3.11.0-23-generic
3.11.0-23 linux-headers-3.11.0-23
3.8.0-35 linux-image-extra-3.8.0-35-generic
3.8.0-35 linux-image-3.8.0-35-generic

5.

Now strip out the current and newer kernel files sed -e “1,/$(uname -r | cut -f1,2 -d”-“)/d” | grep -v -e uname -r | cut -f1,2 -d"-" giving us this:

$ dpkg -l linux-{image,headers}-* | awk '/^ii/{print $2}' | egrep '[0-9]+\.[0-9]+\.[0-9]+' | awk 'BEGIN{FS="-"}; {if ($3 ~ /[0-9]+/) print $3"-"$4,$0; else if ($4 ~ /[0-9]+/) print $4"-"$5,$0}' | sort -k1,1 --version-sort -r | sed -e "1,/$(uname -r | cut -f1,2 -d"-")/d" | grep -v -e `uname -r | cut -f1,2 -d"-"`
3.11.0-23 linux-image-extra-3.11.0-23-generic
3.11.0-23 linux-image-3.11.0-23-generic
3.11.0-23 linux-headers-3.11.0-23-generic
3.11.0-23 linux-headers-3.11.0-23
3.8.0-35 linux-image-extra-3.8.0-35-generic
3.8.0-35 linux-image-3.8.0-35-generic

6

Now strip off the first column we added with awk ‘{print $2}’ to get exactly what we want:

$ dpkg -l linux-{image,headers}-* | awk '/^ii/{print $2}' | egrep '[0-9]+\.[0-9]+\.[0-9]+' | awk 'BEGIN{FS="-"}; {if ($3 ~ /[0-9]+/) print $3"-"$4,$0; else if ($4 ~ /[0-9]+/) print $4"-"$5,$0}' | sort -k1,1 --version-sort -r | sed -e "1,/$(uname -r | cut -f1,2 -d"-")/d" | grep -v -e `uname -r | cut -f1,2 -d"-"` | awk '{print $2}'
linux-image-extra-3.11.0-23-generic
linux-image-3.11.0-23-generic
linux-headers-3.11.0-23-generic
linux-headers-3.11.0-23
linux-image-extra-3.8.0-35-generic
linux-image-3.8.0-35-generic

7

Now we can feed that to the package manager to automatically remove everything and reconfigure grub:

I recommend doing a dry run first (though for your scripting purposes this might not be practical if you have a large environment)

dpkg -l linux-{image,headers}-* | awk '/^ii/{print $2}' | egrep '[0-9]+\.[0-9]+\.[0-9]+' | awk 'BEGIN{FS="-"}; {if ($3 ~ /[0-9]+/) print $3"-"$4,$0; else if ($4 ~ /[0-9]+/) print $4"-"$5,$0}' | sort -k1,1 --version-sort -r | sed -e "1,/$(uname -r | cut -f1,2 -d"-")/d" | grep -v -e `uname -r | cut -f1,2 -d"-"` | awk '{print $2}' | xargs sudo apt-get --dry-run remove

Now if everything looks good go ahead and actually remove it with:

dpkg -l linux-{image,headers}-* | awk '/^ii/{print $2}' | egrep '[0-9]+\.[0-9]+\.[0-9]+' | awk 'BEGIN{FS="-"}; {if ($3 ~ /[0-9]+/) print $3"-"$4,$0; else if ($4 ~ /[0-9]+/) print $4"-"$5,$0}' | sort -k1,1 --version-sort -r | sed -e "1,/$(uname -r | cut -f1,2 -d"-")/d" | grep -v -e `uname -r | cut -f1,2 -d"-"` | awk '{print $2}' | xargs sudo apt-get -y purge

Once again the whole point of this “one-liner” is to remove only the kernels OLDER than the currently running kernel (which leaves any newly installed kernels still available)

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值