-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathremove_old_kernels.sh
64 lines (56 loc) · 1.6 KB
/
remove_old_kernels.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/bin/bash
# Run this script without any param for a dry run
# Run the script with root and with exec param for removing old kernels after checking
# the list printed in the dry run
# Detect OS
if [ -f /etc/os-release ]; then
. /etc/os-release
OS=$ID
else
echo "Unable to detect OS"
exit 1
fi
uname -a
IN_USE=$(uname -r)
echo "Your in use kernel is $IN_USE"
if [[ "$OS" == "ubuntu" || "$OS" == "debian" ]]; then
# For Debian/Ubuntu
echo "Detected Debian-based system"
OLD_KERNELS=$(
dpkg --list |
grep -v "$IN_USE" |
grep -Ei 'linux-image|linux-headers|linux-modules' |
awk '{ print $2 }'
)
echo "Old Kernels to be removed:"
echo "$OLD_KERNELS"
if [ "$1" == "exec" ]; then
for PACKAGE in $OLD_KERNELS; do
yes | apt purge "$PACKAGE"
done
else
echo "If all looks good, run it again like this: sudo remove_old_kernels.sh exec"
fi
elif [[ "$OS" == "centos" || "$OS" == "rhel" || "$OS" == "fedora" ]]; then
# For CentOS/RHEL/Fedora
echo "Detected RedHat-based system"
OLD_KERNELS=$(
rpm -qa |
grep -Ei 'kernel-[0-9]' |
grep -v "$IN_USE" |
sort -V
)
echo "Old Kernels to be removed:"
echo "$OLD_KERNELS"
if [ "$1" == "exec" ]; then
for PACKAGE in $OLD_KERNELS; do
echo "Removing $PACKAGE"
yum -y remove "$PACKAGE" || dnf -y remove "$PACKAGE"
done
else
echo "If all looks good, run it again like this: sudo remove_old_kernels.sh exec"
fi
else
echo "Unsupported OS: $OS"
exit 1
fi