Skip to content

Instantly share code, notes, and snippets.

@margrop
Last active March 23, 2024 11:24
Show Gist options
  • Save margrop/9870fadf8e658fc42a6d9a9fbf3e7259 to your computer and use it in GitHub Desktop.
Save margrop/9870fadf8e658fc42a6d9a9fbf3e7259 to your computer and use it in GitHub Desktop.
Ubuntu 一键移除旧内核脚本
#!/bin/bash
# 当前使用的内核版本
current_kernel=$(uname -r)
echo "当前使用的内核版本是: $current_kernel"
# 列出所有安装的内核,排除当前使用的内核
echo "已安装的其他内核版本:"
installed_kernels=$(dpkg --list | grep linux-image | awk '{ print $2 }' | grep -v "$current_kernel")
# 检查是否有可删除的内核
if [ -z "$installed_kernels" ]; then
echo "没有找到可删除的旧内核。"
exit 0
fi
echo "$installed_kernels"
# 提示用户是否删除
read -p "是否删除上述旧内核? (y/N): " confirm && [[ $confirm == [yY] || $confirm == [yY][eE][sS] ]] || exit 1
# 删除旧内核
echo "正在删除旧内核..."
for kernel in $installed_kernels; do
if [[ $kernel == *"$current_kernel"* ]]; then
echo "跳过当前使用的内核: $kernel"
continue
fi
echo "正在删除: $kernel"
apt remove --purge -y $kernel
done
# 更新引导加载程序
echo "更新引导加载程序..."
update-grub
echo "旧内核删除完成。"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment