Skip to content

Instantly share code, notes, and snippets.

@vifo
Last active October 5, 2018 22:49
Show Gist options
  • Save vifo/6690536 to your computer and use it in GitHub Desktop.
Save vifo/6690536 to your computer and use it in GitHub Desktop.
Cleanup temporary files and caches on Linux Mint 15
#!/usr/bin/env bash
#
# Cleanup temporary files on a Linux Mint 15 Olivia installation.
#
# This script will happily wipe all root/user temporary files, old kernels,
# browser caches and cpan/cpanm build directories. Review before running
# blindly.
#
# Though, so far, there are no Linux Mint 15 Olivia specific cleanups here,
# this script ensures, that we're really running under this distribution. Feel
# free to disable this check.
indent() {
sed -u 's/^/ /'
}
sep() {
echo "-----> $@"
}
OLDCONF=$(dpkg -l|grep "^rc"|awk '{print $2}')
CURKERNEL=$(uname -r|sed 's/-*[a-z]//g'|sed 's/-386//g')
LINUXPKG="linux-(image|headers|ubuntu-modules|restricted-modules)"
METALINUXPKG="linux-(image|headers|restricted-modules)-(generic|i386|server|common|rt|xen)"
OLDKERNELS=$(dpkg -l|awk '{print $2}'|grep -E $LINUXPKG |grep -vE $METALINUXPKG|grep -v $CURKERNEL)
YELLOW="\033[1;33m"
RED="\033[0;31m"
ENDCOLOR="\033[0m"
# Ensure, we're running under Linux Mint 15 Olivia.
if ! $(lsb_release -as 2>/dev/null | grep -q 'Linux Mint 15 Olivia'); then
echo "ERROR: This script is intended to run on Linux Mint 15 Olivia." 1>&2
exit 1
fi
# Root privileges required.
if [ `id -u` != "0" ]; then
echo "ERROR: You must have root privileges in order to run this script." 1>&2
exit 1
fi
sep "Cleaning apt cache"
apt-get clean 2>&1 | indent
sep "Purging configuration files for removed packages"
apt-get remove --purge $OLDCONF 2>&1 | indent
sep "Purging old kernels"
apt-get remove --purge $OLDKERNELS 2>&1 | indent
sep "Emptying trashes"
for d in /home/* /root; do
rm -rf $d/.local/share/Trash/*
done &>/dev/null
sep "Emptying thumbnail caches"
for d in /home/* /root; do
rm -rf $d/.cache/thumbnails/*
done &>/dev/null
sep "Emptying cpan/cpanm build and source directories"
for d in /home/* /root; do
rm -rf $d/.cpan/sources/*
rm -rf $d/.cpan/build/*
rm -rf $d/.cpanm/work/*
rm -rf $d/.cpanm/build.log
rm -rf $d/.cpan/CPAN/MyConfig.pm~
done &>/dev/null
sep "Emptying browser cache directories"
for d in /home/* /root; do
rm -rf $d/.cache/mozilla/firefox/*
rm -rf $d/.cache/google-chrome/*
done &>/dev/null
sep "All done"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment