-
Notifications
You must be signed in to change notification settings - Fork 4
/
purge_kernels.sh
executable file
·43 lines (37 loc) · 1.45 KB
/
purge_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
#!/bin/sh
########################################################################
# purge_kernels.sh: Purge Old Kernels in Ubuntu
#
# Description:
# This script removes old kernels from an Ubuntu or Ubuntu-based system,
# keeping the currently running kernel. It checks for system compatibility
# and only runs on Ubuntu or Ubuntu-based distributions.
#
# Author: id774 (More info: http://id774.net)
# Source Code: https://github.com/id774/scripts
# License: LGPLv3 (Details: https://www.gnu.org/licenses/lgpl-3.0.html)
# Contact: [email protected]
#
# Version History:
# v1.1 2023-12-06
# Refactored for improved readability, naming, and system checking.
# v1.0 2013-11-29
# Initial release.
#
# Usage:
# ./purge_kernels.sh
#
########################################################################
# Check if the system is Ubuntu or Ubuntu-based
if [ ! -f /etc/lsb-release ] || [ ! -f /etc/debian_version ]; then
echo "This script only runs on Ubuntu or Ubuntu-based systems."
exit 1
fi
# Get the current kernel version
CURKERNEL=$(uname -r | sed 's/-*[a-z]//g' | sed 's/-386//g')
LINUXPKG="linux-(image|headers|ubuntu-modules|restricted-modules)"
METALINUXPKG="linux-(image|headers|restricted-modules)-(generic|i386|server|common|rt|xen)"
# Find old kernels except the current one
OLDKERNELS=$(dpkg -l | awk '{print $2}' | grep -E $LINUXPKG | grep -vE $METALINUXPKG | grep -v $CURKERNEL)
# Purge old kernels
sudo aptitude purge $OLDKERNELS