Created
August 25, 2020 08:44
-
-
Save raphendyr/f943cebd2d9a392c38647462b02d6112 to your computer and use it in GitHub Desktop.
Script to remove old linux kernels on debian based distributions
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/sh -e | |
# This script will remove all the kernels, except newest $keep_count and the currently active one. | |
# Very handy with Ubuntus as that installs a lot of kernels. | |
keep_count=${1:-2} | |
# select versions to keep | |
installed=$(dpkg -l 'linux-*' | awk '/^ii/ {print $2}' | grep -E "\-[0-9]+\.[0-9]+\.[0-9]+-[0-9]+" -) | |
keep=$(echo "$installed" | sed -e 's/^[^0-9]\+-//g' -e 's/\(-[0-9]\+\)-[a-z].*$/\1/g' | sort -uVr | head -n $keep_count) | |
rule=$(echo "$keep" | xargs -L1 -I% echo "/%/d;" | tr -d '\n') | |
current=$(uname -r | sed -e 's/\(-[0-9]\+\)-[a-z].*$/\1/g') | |
to_remove=$(echo "$installed" | sed "$rule/$current/d") | |
if [ "$to_remove" ]; then | |
dpkg -P $to_remove | |
else | |
echo "Nothing to remove" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment