Refactor cleanup script to improve disk usage calculation and streamline cache cleaning steps

This commit is contained in:
Laszlo Uyttersprot
2026-01-26 17:25:38 +01:00
parent 16e77aaf92
commit f93509afda

View File

@@ -2,12 +2,20 @@
# Arch Linux cleanup + update script # Arch Linux cleanup + update script
set -euo pipefail set -euo pipefail
# Function to get used space in kilobytes
get_used_space() {
df / --output=used | tail -1
}
echo "==> Calculating initial disk usage..."
PRE_USED=$(get_used_space)
echo "==> Cleaning leftover downloads..." echo "==> Cleaning leftover downloads..."
sudo rm -rf /var/cache/pacman/pkg/*.part sudo rm -rf /var/cache/pacman/pkg/*.part
sudo rm -rf /var/cache/pacman/pkg/download-* sudo rm -rf /var/cache/pacman/pkg/download-*
echo "==> Cleaning package cache..." echo "==> Cleaning package cache..."
yay -Sc --noconfirm yay -Scc --noconfirm
echo "==> Cleaning user cache..." echo "==> Cleaning user cache..."
sudo rm -rf ~/.cache/* sudo rm -rf ~/.cache/*
@@ -16,21 +24,13 @@ echo "==> Updating packages..."
yay -Syu --noconfirm yay -Syu --noconfirm
echo "==> Removing orphaned dependencies..." echo "==> Removing orphaned dependencies..."
yay -Yc --noconfirm if [[ -n $(pacman -Qdtq) ]]; then
yay -Yc --noconfirm
fi
echo "==> Checking for broken packages..." echo "==> Checking for broken packages..."
yay -Dk yay -Dk
echo "==> Cleaning leftover downloads..."
sudo rm -rf /var/cache/pacman/pkg/*.part
sudo rm -rf /var/cache/pacman/pkg/download-*
echo "==> Cleaning package cache..."
yay -Sc --noconfirm
echo "==> Cleaning user cache..."
sudo rm -rf ~/.cache/*
echo "==> Emptying trash..." echo "==> Emptying trash..."
sudo rm -rf ~/.local/share/Trash/files/* sudo rm -rf ~/.local/share/Trash/files/*
sudo rm -rf ~/.local/share/Trash/info/* sudo rm -rf ~/.local/share/Trash/info/*
@@ -38,4 +38,22 @@ sudo rm -rf ~/.local/share/Trash/info/*
echo "==> Cleaning system logs..." echo "==> Cleaning system logs..."
sudo journalctl --vacuum-time=7d sudo journalctl --vacuum-time=7d
echo "==> Calculating final disk usage..."
POST_USED=$(get_used_space)
# Calculate difference
DIFF=$((PRE_USED - POST_USED))
echo "==> Done." echo "==> Done."
if [ "$DIFF" -gt 0 ]; then
# Convert KB to human readable format
CLEANED=$(numfmt --from-unit=1024 --to=iec-i --suffix=B "$DIFF")
echo "Successfully cleared $CLEANED of disk space."
elif [ "$DIFF" -lt 0 ]; then
# If updates were larger than the cleanup
ADDED=$(numfmt --from-unit=1024 --to=iec-i --suffix=B "${DIFF#-}")
echo "Cleanup finished, but system size increased by $ADDED due to updates."
else
echo "Cleanup finished. No change in disk usage."
fi