让我们一一破解代码(对于我的系统):
$ dpkg -l 'linux-image-*'
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name Version Architecture Description
+++-=============================-===================-===================-================================================================
un linux-image-3.0 <none> <none> (no description available)
ii linux-image-3.13.0-32-generic 3.13.0-32.57 amd64 Linux kernel image for version 3.13.0 on 64 bit x86 SMP
ii linux-image-extra-3.13.0-32-g 3.13.0-32.57 amd64 Linux kernel extra modules for version 3.13.0 on 64 bit x86 SMP
ii linux-image-generic 3.13.0.32.38 amd64 Generic Linux kernel image
$ dpkg -l 'linux-image-*' | sed '/^ii/!d'
ii linux-image-3.13.0-32-generic 3.13.0-32.57 amd64 Linux kernel image for version 3.13.0 on 64 bit x86 SMP
ii linux-image-extra-3.13.0-32-generic 3.13.0-32.57 amd64 Linux kernel extra modules for version 3.13.0 on 64 bit x86 SMP
ii linux-image-generic 3.13.0.32.38 amd64 Generic Linux kernel image
$ uname -r | sed 's/\(.*\)-\([^0-9]\+\)/\1/'
3.13.0-32
$ dpkg -l 'linux-image-*' | sed '/^ii/!d' | sed "/$(uname -r | sed 's/\(.*\)-\([^0-9]\+\)/\1/')/d"
ii linux-image-generic 3.13.0.32.38 amd64 Generic Linux kernel image
$ dpkg -l 'linux-image-*' | sed '/^ii/!d' | sed "/$(uname -r | sed 's/\(.*\)-\([^0-9]\+\)/\1/')/d" | sed 's/^[^ ]* [^ ]* \([^ ]*\).*/\1/'
linux-image-generic
$ dpkg -l 'linux-image-*' | sed '/^ii/!d' | sed "/$(uname -r | sed 's/\(.*\)-\([^0-9]\+\)/\1/')/d" | sed 's/^[^ ]* [^ ]* \([^ ]*\).*/\1/' | sed '/[0-9]/!d'
## No output
因此,这无能为力:
$ sudo apt-get remove --purge $(dpkg -l 'linux-image-*' | sed '/^ii/!d;/'"$(uname -r | sed "s/\(.*\)-\([^0-9]\+\)/\1/")"'/d;s/^[^ ]* [^ ]* \([^ ]*\).*/\1/;/[0-9]/!d')
如您所见,这非常接近删除我当前的内核meta包 linux-image-generic 3.13.0.32.38 amd64 Generic Linux kernel image
(由于最后sed '/[0-9]/!d'
一行),这是错误的。
简而言之,这sed
有几个问题。您应该避免使用它来删除较旧的内核。
编辑:
我已经说过该sed
命令有问题。为了扩展起见,我将删除最后两行,其形式实际上应该适用于当前情况。
最后两行采用修改后的形式:
sed -r 's/^[^ ]* *(([a-z]|-)*)(-[a-z]*) *(([0-9]|-)*)\..*/\1-\4\3/'
所以:
$ dpkg -l 'linux-image-*' | sed '/^ii/!d;/'"$(uname -r |
sed "s/\(.*\)-\([^0-9]\+\)/\1/")"'/d' |
sed -r 's/^[^ ]* *(([a-z]|-)*(-[a-z]*) *(([0-9]|\.|-)*)\..*/\1\-\4\3/'
输出:
linux-image-3.13.0.32-generic
多数民众赞成在非常危险!
现在显示--dry-run
完整的apt-get remove
命令,这将对我的系统产生影响:
$ sudo apt-get remove --dry-run $(dpkg -l 'linux-image-*' |
sed '/^ii/!d;/'"$(uname -r | sed "s/\(.*\)-\([^0-9]\+\)/\1/")"'/d' |
sed -r 's/^[^ ]* *(([a-z]|-)*)(-[a-z]*) *(([0-9]|\.|-)*)\..*/\1\-\4\3/')
Reading package lists... Done
Building dependency tree
Reading state information... Done
Note, selecting 'linux-image-3.13.0-32-generic' for regex 'linux-image-3.13.0.32-generic'
The following package was automatically installed and is no longer required:
linux-headers-generic
Use 'apt-get autoremove' to remove it.
The following packages will be REMOVED:
linux-generic linux-image-3.13.0-32-generic
linux-image-extra-3.13.0-32-generic linux-image-generic
0 upgraded, 0 newly installed, 4 to remove and 0 not upgraded.
Remv linux-generic [3.13.0.32.38]
Remv linux-image-generic [3.13.0.32.38]
Remv linux-image-extra-3.13.0-32-generic [3.13.0-32.57]
Remv linux-image-3.13.0-32-generic [3.13.0-32.57]
如您所见,它将删除我所有的当前内核,还请注意,这只是一个模拟,可以匹配我当前的情况,并且sed
可以对原始情况进行改进。
**请注意,如果reboot
像Oli前面提到的那样在内核升级之后进行,则实际代码将起作用。
编辑2:
这是一个简单而健壮的脚本,它将考虑所有情况并删除实际的旧内核:
#!/bin/bash
kernels=( $(grep -Po "^linux-image-[^-]+-[^-]+-generic\b" < <(dpkg --get-selections)) )
cur_rel=$(grep -Po ".*(?=-[a-z]*$)" < <(uname -r))
for kernel in "${kernels[@]}"; do
ker_rel=$(grep -Po "[0-9].*(?=-[a-z]*)" <<< "$kernel")
dpkg --compare-versions "$ker_rel" gt "$cur_rel" && { echo "Please Restart your computer first"; break; }
dpkg --compare-versions "$ker_rel" lt "$cur_rel" && sudo apt-get remove "$kernel"
done
如果您使用的版本比当前版本新,这将向您发出警告,请先重新启动计算机。还要注意,保留旧内核是有充分的理由的,这是一个很好的理由,如果您以某种方式弄乱了当前内核,使系统不稳定,那么您应该能够启动到任何旧内核中。