dotfiles_chezmoi/dot_local/bin/executable_fedora-clean
2026-07-13 15:12:44 -04:00

82 lines
2.3 KiB
Shell

#!/usr/bin/env bash
set -Eeuo pipefail
usage() {
cat <<'USAGE'
Usage: fedora-clean [options]
Clean common Fedora desktop clutter.
Options:
--aggressive Also clear thumbnails, browser crash dumps, and offer Podman prune.
-y, --yes Accept this script's prompts and pass -y where possible.
-h, --help Show this help.
USAGE
}
log() { printf '\n\033[1m==> %s\033[0m\n' "$*"; }
warn() { printf '\033[33mWARN:\033[0m %s\n' "$*" >&2; }
die() { printf '\033[31mERROR:\033[0m %s\n' "$*" >&2; exit 1; }
have() { command -v "$1" >/dev/null 2>&1; }
confirm() {
local prompt="$1" answer
if [[ "${ASSUME_YES}" == "true" ]]; then return 0; fi
read -r -p "$prompt [y/N] " answer
[[ "$answer" =~ ^[Yy]$ ]]
}
DNF="$(command -v dnf5 || command -v dnf || true)"
[[ -n "$DNF" ]] || die "Could not find dnf5 or dnf."
AGGRESSIVE=false
ASSUME_YES=false
while [[ $# -gt 0 ]]; do
case "$1" in
--aggressive) AGGRESSIVE=true ;;
-y|--yes) ASSUME_YES=true ;;
-h|--help) usage; exit 0 ;;
*) die "Unknown option: $1" ;;
esac
shift
done
YUM_YES=()
[[ "$ASSUME_YES" == "true" ]] && YUM_YES=(-y)
log "Disk usage before cleanup"
df -hT -x tmpfs -x devtmpfs / "$HOME" 2>/dev/null || df -hT -x tmpfs -x devtmpfs
log "Removing unused RPM packages"
sudo "$DNF" autoremove "${YUM_YES[@]}" || warn "DNF autoremove did not complete. Review the output above."
log "Cleaning downloaded DNF packages"
sudo "$DNF" clean packages || warn "DNF clean packages failed."
if have flatpak; then
log "Removing unused Flatpak runtimes"
flatpak uninstall --unused "${YUM_YES[@]}" || warn "Flatpak unused-runtime cleanup did not complete."
fi
if have journalctl; then
log "Current journal disk usage"
journalctl --disk-usage || true
if confirm "Vacuum systemd journal to 14 days?"; then
sudo journalctl --vacuum-time=14d
fi
fi
if [[ "$AGGRESSIVE" == "true" ]]; then
log "Aggressive user-cache cleanup"
if confirm "Clear GNOME thumbnail cache?"; then
rm -rf -- "$HOME/.cache/thumbnails"/* 2>/dev/null || true
fi
if confirm "Clear user crash-report cache under ~/.cache/abrt?"; then
rm -rf -- "$HOME/.cache/abrt"/* 2>/dev/null || true
fi
if have podman && confirm "Prune unused Podman images/containers/networks?"; then
podman system prune -a
fi
fi
log "Disk usage after cleanup"
df -hT -x tmpfs -x devtmpfs / "$HOME" 2>/dev/null || df -hT -x tmpfs -x devtmpfs