Skip to content

Instantly share code, notes, and snippets.

@bulletmark
Created February 26, 2023 23:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bulletmark/fd8ff593e99d6728ce3afb451d128f74 to your computer and use it in GitHub Desktop.
Save bulletmark/fd8ff593e99d6728ce3afb451d128f74 to your computer and use it in GitHub Desktop.
Arch Linux script to clean system
PROGARGS="$*"
JTIME="2weeks"
# First AUR helper found in this list will be used
AURHELPERS="yay paru trizen pacaur"
usage() {
echo "Usage: $(basename $PROG) [options]"
echo "Clean system and user package caches and prune journal."
echo "Options:"
echo " -r (run as root. Do NOT use this, executed only programatically)"
echo " -a <AUR HELPER>"
exit 1
}
ROOT=0
AURHELP=""
while getopts ra: c; do
case $c in
r) ROOT=1;;
a) AURHELP=$OPTARG;;
\?) usage;;
esac
done
shift $((OPTIND - 1))
if [[ $# -ne 0 ]]; then
usage
fi
if [[ -n $AURHELP ]]; then
if ! type $AURHELP &>/dev/null; then
echo "ERROR: $AURHELP must be installed."
exit 1
fi
else
for h in $AURHELPERS; do
if type $h &>/dev/null; then
AURHELP=$h
break
fi
done
if [[ -z $AURHELP ]]; then
echo "ERROR: None of \"$AURHELPERS\" are installed."
exit 1
fi
fi
run_as_user() {
echo "### Cleaning $AURHELP USER cache of uninstalled AUR packages .."
$AURHELP -Scq --color=never --noconfirm
echo "### Removing orphan packages .."
LIST="$($AURHELP -Qtdq)"
if [[ -n $LIST ]]; then
$AURHELP -Rns $LIST --noconfirm
fi
}
run_as_root() {
echo "### Cleaning pacman SYSTEM cache of uninstalled packages .."
paccache -ruk0 --nocolor
echo
echo "### Cleaning pacman SYSTEM cache of installed packages .."
paccache -rk2 --nocolor
echo
echo "### Pruning journal to last $JTIME"
journalctl --vacuum-time=$JTIME
}
if [[ $ROOT -eq 0 ]]; then
if [[ $(id -u) == 0 ]]; then
echo "Do not manually run as root." >&2
exit 1
fi
run_as_user
exec sudo $PROG -r $PROGARGS
else
if [[ $(id -u) != 0 ]]; then
echo "Do not manually specify root mode." >&2
exit 1
fi
if [[ -z "$SUDO_USER" ]]; then
echo "Must run with sudo to identify admin user $AURHELP cache dir." >&2
exit 1
fi
run_as_root
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment