删除Ubuntu Linux旧内核的方法

sh/bash/dash/ksh/zsh等Shell脚本
头像
yuzh652800
帖子: 686
注册时间: 2008-03-12 8:41
来自: nenu.Ani

Re: 删除Ubuntu Linux旧内核的方法

#16

帖子 yuzh652800 » 2011-04-05 9:59

kubuntu下ubuntu-tweak好像不怎么好使,只能用老办法了。
不能假定每个人都爱用命令行。
只要支持Linux的,我们都应该支持。
糖衣炮弹
帖子: 25
注册时间: 2008-04-26 23:27
联系:

Re: 删除Ubuntu Linux旧内核的方法

#17

帖子 糖衣炮弹 » 2011-04-12 20:21

o哦拉,问题解决,谢谢楼主。
真操作过程中貌似grub.cfg不用修改,运行完卸载命令就没有了。
头像
eexpress
帖子: 58428
注册时间: 2005-08-14 21:55
来自: 长沙

Re: 删除Ubuntu Linux旧内核的方法

#18

帖子 eexpress » 2011-04-12 20:24

代码: 全选

sudo aptitude purge ~ilinux-.*\(\!`uname -r|cut -d- -f1-2`\)~i[0-9]
● 鸣学
头像
tenzu
论坛版主
帖子: 36924
注册时间: 2008-11-21 20:26

Re: 删除Ubuntu Linux旧内核的方法

#19

帖子 tenzu » 2011-04-12 20:27

又见神码 :em70
nmimi
帖子: 20
注册时间: 2013-12-28 21:23
系统: Win7+Debian
来自: 天朝魔都
联系:

Re: 删除Ubuntu Linux旧内核的方法

#20

帖子 nmimi » 2014-01-19 22:06

eexpress 写了:

代码: 全选

sudo aptitude purge ~ilinux-.*\(\!`uname -r|cut -d- -f1-2`\)~i[0-9]
这段命令写不出来。
这个人很懒,啥都没留下,连签名都没有,鄙视。
纯广告,勿点。
xyq164288
帖子: 80
注册时间: 2009-01-25 15:13

Re: 删除Ubuntu Linux旧内核的方法

#21

帖子 xyq164288 » 2014-05-27 17:58

eexpress 写了:

代码: 全选

sudo aptitude purge ~ilinux-.*\(\!`uname -r|cut -d- -f1-2`\)~i[0-9]

已试用!确实神码!!!
头像
youzhiyili
帖子: 2422
注册时间: 2012-03-22 20:42
系统: ubuntu22.04

Re: 删除Ubuntu Linux旧内核的方法

#22

帖子 youzhiyili » 2014-05-27 18:50

呵呵,对比老外的方法,E神的的确最短 :em11

方法一:

代码: 全选

sudo apt-get -y purge $(dpkg --get-selections | awk '((/^linux-/) && (/[0-9]\./) && (!/'"`uname -r | sed "s/-generic//g"`"'/)) {print $1}')
方法二:

代码: 全选

sudo dpkg -l 'linux-*' | sed '/^ii/!d;/'"$(uname -r | sed "s/\(.*\)-\([^0-9]\+\)/\1/")"'/d;s/^[^ ]* [^ ]* \([^ ]*\).*/\1/;/[0-9]/!d' | xargs sudo apt-get -y purge
方法三:

代码: 全选

#/bin/bash
ls /boot/ | grep vmlinuz | sed 's@vmlinuz-@linux-image-@g' | grep -v `uname -r` > /tmp/kernelList
for I in `cat /tmp/kernelList`
do
aptitude remove $I
done
rm -f /tmp/kernelList
update-grub
方法四:(国产交互式的)

代码: 全选

#!/bin/bash
#Program:
# Let user uninstall unused kernels which installed as debian package form.
#Author:
# mtyy110
#ubuntu删除旧内核
dialog=whiptail
if [ "`whoami`" != 'root' ]; then
echo '需要root权限'
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 "要删除哪个版本?(0 退出)" 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 "格式不对,请输入正确的版本格式"
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 "没发现这个版本:$version"
continue
fi 


tmp=`uname -r | grep "$version" | wc -l`
if [ $tmp -eq 1 ]; then
read -p "`uname -r` 是当前正在使用的内核,确定要删除?(y/N)" choice
if [ "$choice" != 'y' -a "$choice" != 'Y' ]; then
continue
fi 
fi 


if [ $total -le $sum ]; then
read -p "确定要卸载所有内核?(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 "是否继续删除其他的内核?(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
@自由建客 @qy117121 @枫叶饭团 @cikekid @YeLee @jtshs256 @eexpress @ljj_jjl2008 @zhw2101024 @TeliuTe @月下叹逍遥 @leeaman @懒蜗牛Gentoo @灰色小狼 @photor @adagio @JiangHui @phoenixlzx @oneleaf
回复