ZhangZhihui's Blog  

找出系统已经安装的内核版本,在终端里输入命令:

dpkg --get-selections | grep linux-image

然后会显示系统中已安装的内核,例如:

linux-image-2.6.38-10-generic            instal
linux-image-2.6.38-8-generic            install
linux-image-2.6.35-22-generic            install
linux-image-generic                install

2.卸载旧的内核版本,在终端里输入命令:

sudo apt-get remove linux-image-2.6.35-22-generic linux-image-2.6.38-8-generic

上面命令和含义是:

dpkg --get-selections [<表达式> ...] 把已选中的软件包列表打印到标准输出;

grep linux-image 匹配查找;

uname -a 查看已安装的linux内核版。

这样,旧的内核版本就删除了。

 

zzh@ZZHPC:~$ sudo dpkg --get-selections | grep linux-image
linux-image-6.8.0-52-generic			install
linux-image-6.8.0-57-generic			install
linux-image-generic-hwe-22.04			install
zzh@ZZHPC:~$ dpkg --get-selections | grep linux-headers
linux-headers-6.8.0-52-generic			install
linux-headers-6.8.0-57-generic			install
linux-headers-generic-hwe-22.04			install
zzh@ZZHPC:~$ sudo dpkg --get-selections | grep linux-modules-extra
linux-modules-extra-6.8.0-52-generic		install
linux-modules-extra-6.8.0-57-generic		install

 

#!/bin/bash

echo "🔍 Cleaning up old Linux kernel packages..."

# Step 1: Purge deinstall-marked linux-image packages
echo "📦 Purging deinstalled linux-image packages..."
dpkg --get-selections | grep 'linux-image' | grep deinstall | awk '{print $1}' | xargs -r sudo dpkg --purge

# Step 2: Find current kernel version
current_kernel=$(uname -r | cut -d'-' -f1,2)
echo "🧠 Current running kernel: $current_kernel"

# Step 3: Remove unused linux headers/modules (except the current one)
echo "🧼 Removing old linux-headers, linux-modules, and extras..."
for pkg in $(dpkg --get-selections | grep -E 'linux-(headers|modules|modules-extra)' | awk '{print $1}'); do
    if [[ "$pkg" != *"$current_kernel"* ]]; then
        echo "Purging: $pkg"
        sudo apt purge -y "$pkg"
    fi
done

# Step 4: Autoremove
echo "🧹 Running autoremove..."
sudo apt autoremove --purge -y

# Step 5: Clean apt cache
echo "🧽 Cleaning apt cache..."
sudo apt clean

echo "✅ Cleanup complete!"

 

Copied from: https://blog.csdn.net/stevejobson/article/details/50531659

posted on 2020-09-01 22:15  ZhangZhihuiAAA  阅读(1223)  评论(0)    收藏  举报