Created
January 18, 2023 12:05
-
-
Save sdon2/6462697644ef186bd10ec38f253caafa to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash -e | |
# Run this script without any arguments for a dry run | |
# Run the script with root and with exec arguments for removing old kernels and modules after checking | |
# the list printed in the dry run | |
uname -a | |
IN_USE=$(uname -a | awk '{ print $3 }') | |
echo "Your in use kernel is $IN_USE" | |
OLD_KERNELS=$( | |
dpkg --get-selections | | |
grep -v "linux-headers-generic" | | |
grep -v "linux-image-generic" | | |
grep -v "linux-image-generic" | | |
grep -v "${IN_USE%%-generic}" | | |
grep -Ei 'linux-image|linux-headers|linux-modules' | | |
awk '{ print $1 }' | |
) | |
echo "Old Kernels to be removed:" | |
echo "$OLD_KERNELS" | |
OLD_MODULES=$( | |
ls /lib/modules | | |
grep -v "${IN_USE%%-generic}" | | |
grep -v "${IN_USE}" | |
) | |
echo "Old Modules to be removed:" | |
echo "$OLD_MODULES" | |
if [ "$1" == "exec" ]; then | |
apt-get purge $OLD_KERNELS | |
for module in $OLD_MODULES ; do | |
rm -rf /lib/modules/$module/ | |
done | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment