Skip to content

Instantly share code, notes, and snippets.

@gorango
Created July 8, 2015 20:47
Show Gist options
  • Save gorango/90c789a504f3011da832 to your computer and use it in GitHub Desktop.
Save gorango/90c789a504f3011da832 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Ubuntu kernel cleanup:
# Save the 2 latest kernels and remove the old
IMAGES=($(dpkg --get-selections | grep "linux-image-[[:digit:]].*" | tr "\t" ";" | cut -d ";" -f1))
HEADERS=($(dpkg --get-selections | grep "linux-headers-[[:digit:]].*" | tr "\t" ";" | cut -d ";" -f1))
I_LEN=${#IMAGES[@]}
H_LEN=${#HEADERS[@]}
if [ $I_LEN>2 ] ; then
for (( i=0; i<(${I_LEN}-2); i++ )) ; do
sudo apt-get purge ${IMAGES[$i]}
done
sudo apt-get autoclean && sudo apt-get autoremove
fi
if [ $H_LEN>2 ] ; then
for (( i=0; i<(${H_LEN}-2); i++ )) ; do
sudo apt-get purge ${HEADERS[$i]}
done
sudo apt-get autoclean && sudo apt-get autoremove
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment