Files
RankzyLinux/clean.sh

59 lines
1.5 KiB
Bash
Executable File

#!/bin/bash
# Arch Linux cleanup + update script
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..."
sudo rm -rf /var/cache/pacman/pkg/*.part
sudo rm -rf /var/cache/pacman/pkg/download-*
echo "==> Cleaning package cache..."
yay -Scc --noconfirm
echo "==> Cleaning user cache..."
sudo rm -rf ~/.cache/*
echo "==> Updating packages..."
yay -Syu --noconfirm
echo "==> Removing orphaned dependencies..."
if [[ -n $(pacman -Qdtq) ]]; then
yay -Yc --noconfirm
fi
echo "==> Checking for broken packages..."
yay -Dk
echo "==> Emptying trash..."
sudo rm -rf ~/.local/share/Trash/files/*
sudo rm -rf ~/.local/share/Trash/info/*
echo "==> Cleaning system logs..."
sudo journalctl --vacuum-time=7d
echo "==> Calculating final disk usage..."
POST_USED=$(get_used_space)
# Calculate difference
DIFF=$((PRE_USED - POST_USED))
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