os:ubuntu2204 -- 删除“旧内核”脚本
一、ubuntu删除旧内核:
1、查询内核信息:
1 [wit@on:null]$ sudo dpkg --get-selections | grep linux
2、删除旧内核:
1 [wit@on:null]$ sudo apt purge OLD-KERNEL
1 删除内核:“6.5.0-9”
2
3 [wit@ubuntu:null]$ sudo dpkg --get-selections | grep linux | grep -E "*-6.5.0-9-*" | awk '{print $1}' | xargs -n 1 sudo apt purge -y
3、删除信息“deinstall”:
1 [wit@on:null]$ sudo dpkg --get-selections | grep linux | grep deinstall | awk '{ print $1}' | sudo dpkg -P
4、更新启动项内容(更新grub):
1 [wit@on:null]$ sudo update-grub
二、删除旧内核的脚本源码
1 #!/usr/bin/env bash
2
3
4
5
6 # filename = remove_old_kernel_ubuntu
7
8
9
10
11 # remove_old_kernel_ubuntu_help
12
13 function remove_old_kernel_ubuntu_help()
14 {
15
16 echo
17 echo
18 echo -e "\n\t== remove_old_kernel_ubuntu_help: begin =="
19 echo -e ' command parameter1 function'
20 echo -e ' remove_old_kernel_ubuntu null -- List all kernel files installed.'
21 echo -e ' remove_old_kernel_ubuntu kernel-old-version -- Remove the kernel files matched with "kernel-old-version" .'
22 echo
23 echo -e ' == examples =='
24 echo -e ' remove_old_kernel_ubuntu List all kernel files installed. '
25 echo -e ' remove_old_kernel_ubuntu 6.2.0-36 Remove kernel files matched with "6.2.0-36" .'
26 echo -e ' [wit@on:tools]$ ./remove_old_kernel_ubuntu 6.2.0-36'
27 echo -e "\t== remove_old_kernel_ubuntu_help: begin ==\n"
28 echo
29 echo
30
31
32 }
33
34
35
36 # display kernel files by current using
37
38 function current_use_kernel()
39 {
40
41 echo -e "\n\t-- current_use_kernel: begin --";
42
43 msg=$(echo "ROOT_MIMA" | sudo -S uname -srmo)
44 echo -e "\t\t" ${msg}
45
46 echo -e "\t-- current_use_kernel: end --\n";
47
48 }
49
50
51
52
53 # list kernel files and return a parameter of type of array.
54
55 function list_kernel()
56 {
57
58 echo -e "\n\t-- list_kernel: begin --";
59
60 i=1
61 for f in $(echo "ROOT_MIMA" | sudo -S dpkg --get-selections | grep -E "^linux" | awk '{ print $1}')
62 do
63 echo -e "\t\tLIST_FILE_$i := " ${f}
64 i=$(($i+1))
65 done
66
67 echo -e "\t-- list_kernel: over --\n";
68
69 }
70
71
72
73
74
75 # delete files of old kernels
76
77 function delete_old_kernel_files()
78 {
79
80
81 if [ $1 ]
82 then
83
84 echo -e "\n\t-- delete_old_kernel_files: begin --";
85
86 echo -e "\n\t\tREMOVLE_OLD_KERNEL := " $1 "\n"
87
88 kernel_files=$(echo "ROOT_MIMA" | sudo -S dpkg --get-selections | grep -E "^linux.*$1-?*" | awk '{ print $1}')
89
90 i=1
91 for f in ${kernel_files}
92 do
93 echo -e "\n\n\t\tDELETE_FILE_$i := " ${f} "\n"
94 echo "ROOT_MIMA" | sudo -S apt purge -y ${f}
95 i=$(($i+1))
96 done
97
98 echo -e "\t-- delete_old_kernel_files: over --\n";
99
100 else
101
102 echo -e "\t[ ERROR ]:\t" "\$1 is NULL, this programe is over !"
103 exit 3
104
105 fi
106
107 }
108
109
110
111
112 # fina_work(): remove old information of old kernel, and then clean, and then update-grub
113
114 function final_work()
115 {
116
117 echo -e "\n\t-- final_work: begin --\n";
118 #clean_files=$(echo "ROOT_MIMA" | sudo -S dpkg --get-selections | grep linux | grep reinstall | awk '{ print $1}')
119 clean_files=$(echo "ROOT_MIMA" | sudo -S dpkg --get-selections | grep linux | grep reinstall | awk '{ print $1}')
120
121 if [ ${clean_files} ]
122 then
123 for f in ${clean_files}
124 do
125 echo echo "ROOT_MIMA" | sudo -S dpkg -P ${f}
126 done
127 fi
128
129 echo "ROOT_MIMA" | sudo -S apt autoremove -y;
130 #echo "ROOT_MIMA" | sudo -S update-grub;
131
132 echo -e "\n\t-- final_work: over --\n";
133 }
134
135
136
137
138 # run
139
140 function run()
141 {
142
143 echo
144 echo -e "\n== remove_old_kernel_ubuntu: begin ==\n";
145
146
147 # remove_old_kernel_ubuntu_help
148 remove_old_kernel_ubuntu_help
149
150 # display current kernel
151 current_use_kernel
152
153 # list all files of kernel
154 list_kernel
155
156 # delete all files of kernel
157 if [ $1 ]
158 then
159 delete_old_kernel_files $1
160
161 # final_work
162 final_work
163 fi
164
165 # list all files of kernel
166 list_kernel
167
168 # display current kernel
169 current_use_kernel
170
171 echo -e "\n== remove_old_kernel_ubuntu: over ==\n";
172 echo
173
174 }
175
176
177
178
179 run $1
三、参考内容:
1、ubuntu中删除显示为deinstall的内核 https://codeleading.com/article/15734293507/
2、删除多余的ubutu内核 https://codeleading.com/article/33442159805/
本文由 lnlidawei 原创、整理、转载,本文来自于【博客园】; 整理和转载的文章的版权归属于【原创作者】; 转载或引用时请【保留文章的来源信息】:https://www.cnblogs.com/lnlidawei/p/17814188.html