Skip to content

Instantly share code, notes, and snippets.

@agirault
Created May 16, 2024 15:18
Show Gist options
  • Save agirault/eaa0395951f982f9844fdcb8dac35415 to your computer and use it in GitHub Desktop.
Save agirault/eaa0395951f982f9844fdcb8dac35415 to your computer and use it in GitHub Desktop.
#!/bin/bash
set -eu
function current_kernel_version() {
uname -r | sed -r 's/-[a-z]+//'
}
function kernels() {
dpkg -l linux-{image,headers,modules,hwe,modules-extra}-"[0-9]*" | awk '/ii/{print $2}'
}
function current_kernel() {
kernels | grep "$(current_kernel_version)"
}
function unused_kernels() {
kernels | grep -v "$(current_kernel_version)"
}
function remove_unused_kernels() {
sudo apt-get purge $(unused_kernels)
sudo apt autoremove
}
function update_kernel() {
sudo apt-get install linux-generic
sudo apt autoremove
}
function print_usage() {
echo "USAGE: $0 [cmd]"
echo
echo " version - version of the current linux kernel"
echo " current - current linux kernel"
echo " unused - unused linux kernels"
echo " remove_unused - delete unused linux kernels"
echo " update - update linux kernel"
}
function main() {
if [ -z "${1-}" ]; then
print_usage
return 0
fi
case "$1" in
-v|v|version)
current_kernel_version
;;
-c|c|current)
current_kernel
;;
-u|u|unused)
unused_kernels
;;
-rm|rm|remove_unused)
remove_unused_kernels
;;
update)
update_kernel
;;
*)
print_usage
return 1
;;
esac
}
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
main $@
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment