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)