Skip to content

Instantly share code, notes, and snippets.

@o3bvv
Last active May 8, 2021 06:22
Show Gist options
  • Save o3bvv/3b1d2e090bbc0f71104f73ec8c959856 to your computer and use it in GitHub Desktop.
Save o3bvv/3b1d2e090bbc0f71104f73ec8c959856 to your computer and use it in GitHub Desktop.
Delete old kernels from Ubuntu
#!/bin/sh
# 1. List old versions by:
# 1.1. Listing all "initrd.img-*" files in "/boot" (ls -1), ex: "/boot/initrd.img-5.8.0-44-generic"
# 1.2. Excluding current LOADED version (grep -v), ex: "5.8.0-50-generic"
# 1.3. Extracting version part from listed filenames (awk), ex: "5.8.0-44"
# 2. Purge kernel packages per each version, with confirmation prompt (configmation is enabled by default)
for v in $(ls -1 /boot/initrd.img-* | grep -v `uname -r` | awk -F'-' '{ print $2 "-" $3 }'); do \
sudo apt-get --purge remove \
linux-image-$v-generic \
linux-modules-$v-generic \
linux-headers-$v \
linux-headers-$v-generic; \
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment