From f5ada8441c6e9618013af66ae41aedbfa6a53ac2 Mon Sep 17 00:00:00 2001 From: Sandor Semsey Date: Sun, 10 Sep 2023 11:39:17 +0200 Subject: [PATCH 1/8] clean: use sudo instead requiring running by root --- bin/clean | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/bin/clean b/bin/clean index 933a117..245488e 100755 --- a/bin/clean +++ b/bin/clean @@ -15,27 +15,21 @@ journal_retention=2w set -eufo pipefail IFS=$'\n\t' -user_id=$(id -u) -if [[ "${user_id}" -ne 0 ]]; then - echo "Run as root!" - exit 1 -fi - # apt -apt-get autoremove --purge --yes -apt-get clean --yes +sudo apt-get autoremove --purge --yes +sudo apt-get clean --yes # snap for line in $(snap list --all | awk '/disabled/{print $1, $3}'); do IFS=$' \t' read -r snapname revision <<< "${line}" - snap remove "${snapname}" --revision="${revision}" + sudo snap remove "${snapname}" --revision="${revision}" done # Rotate journal -journalctl --flush --rotate --vacuum-time="${journal_retention}" +sudo journalctl --flush --rotate --vacuum-time="${journal_retention}" # cache -find /var/cache -mindepth 1 -delete -find /var/lib/snapd/cache -mindepth 1 -delete +sudo find /var/cache -mindepth 1 -delete +sudo find /var/lib/snapd/cache -mindepth 1 -delete exit 0 From 8083859fcd57d1d37d238fea84faeed9ca1e0202 Mon Sep 17 00:00:00 2001 From: Sandor Semsey Date: Sun, 10 Sep 2023 11:40:23 +0200 Subject: [PATCH 2/8] clean: add --purge to snap remove --- bin/clean | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/clean b/bin/clean index 245488e..6f0778f 100755 --- a/bin/clean +++ b/bin/clean @@ -22,7 +22,7 @@ sudo apt-get clean --yes # snap for line in $(snap list --all | awk '/disabled/{print $1, $3}'); do IFS=$' \t' read -r snapname revision <<< "${line}" - sudo snap remove "${snapname}" --revision="${revision}" + sudo snap remove "${snapname}" --purge --revision="${revision}" done # Rotate journal From 5a0ac65decddb3ad62d8af41026867448543930d Mon Sep 17 00:00:00 2001 From: Sandor Semsey Date: Sun, 10 Sep 2023 11:47:14 +0200 Subject: [PATCH 3/8] clean: add usage --- bin/clean | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 75 insertions(+), 2 deletions(-) diff --git a/bin/clean b/bin/clean index 6f0778f..2352b1f 100755 --- a/bin/clean +++ b/bin/clean @@ -8,13 +8,86 @@ ## Delete caches ## ############################## -# Config -journal_retention=2w +############### +## FUNCTIONS ## +############### + +## Version +########## +_version() { + cat < Date: Sun, 10 Sep 2023 12:03:33 +0200 Subject: [PATCH 4/8] clean: implement --quiet --- bin/clean | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/bin/clean b/bin/clean index 2352b1f..a2c5ee5 100755 --- a/bin/clean +++ b/bin/clean @@ -55,6 +55,7 @@ IFS=$'\n\t' journal_retention=2w users=() quiet=() +redirection=/dev/stdout while :; do case "${1:-}" in @@ -68,6 +69,7 @@ while :; do ;; -q | --quiet) quiet=(--quiet) + redirection=/dev/null ;; -h | --help) _version @@ -89,17 +91,18 @@ while :; do done # apt -sudo apt-get autoremove --purge --yes -sudo apt-get clean --yes +sudo apt-get autoremove --purge --yes "${quiet[@]}" "${quiet[@]}" +sudo apt-get clean --yes "${quiet[@]}" "${quiet[@]}" # snap for line in $(snap list --all | awk '/disabled/{print $1, $3}'); do IFS=$' \t' read -r snapname revision <<< "${line}" - sudo snap remove "${snapname}" --purge --revision="${revision}" + # shellcheck disable=SC2024 + sudo snap remove "${snapname}" --purge --revision="${revision}" > "${redirection}" done # Rotate journal -sudo journalctl --flush --rotate --vacuum-time="${journal_retention}" +sudo journalctl --flush --rotate --vacuum-time="${journal_retention}" "${quiet[@]}" # cache sudo find /var/cache -mindepth 1 -delete From 15ba4056a2e6735c70e13962b26a50b4a276ce62 Mon Sep 17 00:00:00 2001 From: Sandor Semsey Date: Sun, 10 Sep 2023 12:04:18 +0200 Subject: [PATCH 5/8] clean: add failsafe to cache clear --- bin/clean | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bin/clean b/bin/clean index a2c5ee5..077cca6 100755 --- a/bin/clean +++ b/bin/clean @@ -104,8 +104,8 @@ done # Rotate journal sudo journalctl --flush --rotate --vacuum-time="${journal_retention}" "${quiet[@]}" -# cache -sudo find /var/cache -mindepth 1 -delete -sudo find /var/lib/snapd/cache -mindepth 1 -delete +# System caches +[[ -d /var/cache ]] && sudo find /var/cache -mindepth 1 -delete +[[ -d /var/lib/snapd/cache ]] && sudo find /var/lib/snapd/cache -mindepth 1 -delete exit 0 From 3f7e8fb05d509a6ca6f2f621040715b937181677 Mon Sep 17 00:00:00 2001 From: Sandor Semsey Date: Sun, 10 Sep 2023 12:28:25 +0200 Subject: [PATCH 6/8] clean: implement --user --- bin/clean | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/bin/clean b/bin/clean index 077cca6..db0993f 100755 --- a/bin/clean +++ b/bin/clean @@ -108,4 +108,25 @@ sudo journalctl --flush --rotate --vacuum-time="${journal_retention}" "${quiet[@ [[ -d /var/cache ]] && sudo find /var/cache -mindepth 1 -delete [[ -d /var/lib/snapd/cache ]] && sudo find /var/lib/snapd/cache -mindepth 1 -delete +# User caches +for user in "${users[@]}"; do + if ! homedir=$(getent passwd "${user}" | cut -d: -f6); then + echo "User not exits: ${user}" 1>&2 + exit 1 + fi + + # Empty trash bin + sudo test -d "${homedir}/.local/share/Trash" && sudo find "${homedir}/.local/share/Trash" -mindepth 1 -delete + # General cache + sudo test -d "${homedir}/.cache" && sudo find "${homedir}/.cache" -mindepth 1 -delete + # Atom cache + sudo test -d "${homedir}/.atom/.apm/_cacache" && sudo find "${homedir}/.atom/.apm/_cacache" -mindepth 1 -delete + # Chrome cache + sudo test -d "${homedir}/.config/google-chrome" && sudo find "${homedir}/.config/google-chrome" -mindepth 1 -delete + # gdfuse (Google Drive FS) cache + sudo test -d "${homedir}/.gdfuse/${user}/cache" && sudo find "${homedir}/.gdfuse/${user}/cache" -mindepth 1 -delete + # Flush Spotify cache + sudo test -d "${homedir}/snap/spotify/common/.cache" && sudo find "${homedir}/snap/spotify/common/.cache" -mindepth 1 -delete +done + exit 0 From 2555450d5028f526bd76c6beb547c3512dc42aa4 Mon Sep 17 00:00:00 2001 From: Sandor Semsey Date: Sun, 10 Sep 2023 12:33:22 +0200 Subject: [PATCH 7/8] clean: docs --- docs/bin.md | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/docs/bin.md b/docs/bin.md index 40525c1..319958d 100644 --- a/docs/bin.md +++ b/docs/bin.md @@ -121,7 +121,7 @@ Clean-up temporary and other not needed files to free up disk space: - Remove unused `apt` packages - Remove old snaps - Rotate system journal (keep 2 weeks of logs) -- Delete caches +- Delete caches (various system & user caches) !!! info @@ -131,7 +131,18 @@ Clean-up temporary and other not needed files to free up disk space: **Usage** ``` -Usage: clean +Usage: clean [OPTIONS]... + +OPTIONS + +-j, --journal [TIME] Remove journal older than this time + specified with the usual "s", "m", "h", etc. suffixes. + Defaults to 2 weeks (2w). +-u, --user [USER] Clear temp files in user's HOME also. + Can be specified multiple times to clean more user HOME. +-q, --quiet Suppress non-error messages +-h, --help Display this help +-v, --version Print version info ``` --- From 708e6a5c35c95b621e1ae68a11c6bd5cd417c707 Mon Sep 17 00:00:00 2001 From: Sandor Semsey Date: Sun, 10 Sep 2023 12:34:28 +0200 Subject: [PATCH 8/8] use consistent usage --- bin/backup | 48 ++++++++++++++++++++++++------------------------ bin/benchmark | 34 +++++++++++++++++----------------- bin/pass-man | 8 ++++---- 3 files changed, 45 insertions(+), 45 deletions(-) diff --git a/bin/backup b/bin/backup index 94c5767..5c4b401 100755 --- a/bin/backup +++ b/bin/backup @@ -20,8 +20,8 @@ ########## _version() { cat <