Skip to content

Instantly share code, notes, and snippets.

@jeenuv
Created September 23, 2014 10:37
Show Gist options
  • Select an option

  • Save jeenuv/f82dc9c21b7f3be2ddcf to your computer and use it in GitHub Desktop.

Select an option

Save jeenuv/f82dc9c21b7f3be2ddcf to your computer and use it in GitHub Desktop.
Remove old kernel and header packages
#!/bin/bash
# Script to remove old kernel image and header packages. The script only picks
# up packages that are older than the current kernel (so as to not to remove the
# current or any recently-installed kernels
current_version="$(uname -a | awk '{print $3}')" # 3.2.0-69-generic
cv_1="$(echo "$current_version" | awk -F- '{print $1}')" # 3.2.0
cv_2="$(echo "$current_version" | awk -F- '{print $2}')" # 69
pkg_list="$(mktemp)"
# Get all image and headers packages that are older than the current one
dpkg -l 'linux-image-*' 'linux-headers-*' | awk -v cv_1="$cv_1" -v cv_2="$cv_2" '
$1 == "ii" {
pkg = $2
ofs = FS
FS = "-"
$0 = $0
# Compare cv_2 less than because we do not want to remove the current package
if ($3 <= cv_1 && $4 < cv_2)
print pkg
FS = ofs
}' > "$pkg_list"
# Check whether we've any packages to remove
if [ "$(wc -l <"$pkg_list")" = "0" ]; then
echo "No packages to remove"
exit
else
echo "List of packages to remove"
sed 's/^/\t/' < "$pkg_list"
fi
# This presumably will prompt the user for removal
sudo apt-get remove $(cat "$pkg_list")
@jeenuv
Copy link
Author

jeenuv commented May 26, 2018

Superseded by cleanup_linux_pkg.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment