Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 14 additions & 43 deletions bin/clean
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
##########
_version() {
cat <<VERSION
clean v1.3.0
clean v2.0.1
Written by Sandor Semsey, Copyright (C) 2023, License MIT
VERSION
}
Expand All @@ -28,14 +28,12 @@ _usage() {

Usage: clean [OPTIONS]...

Clean-up temporary and other unused files to free up disk space
Clean-up temporary and other unused files to free up disk space. Need root permissions.

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
Expand Down Expand Up @@ -63,7 +61,6 @@ IFS=$'\n\t'
# Parse options
###############
journal_retention=2w
users=()
quiet=()
redirection=/dev/stdout

Expand All @@ -73,10 +70,6 @@ while :; do
shift
journal_retention="${1}"
;;
-u | --user)
shift
users+=("${1}")
;;
-q | --quiet)
quiet=(--quiet)
redirection=/dev/null
Expand All @@ -100,54 +93,32 @@ while :; do
shift
done

uid=$(id -u)
if [[ "${uid}" -ne 0 ]]; then
echo Run as root! >&2
exit 1
fi

print Clean APT...
sudo apt-get "${quiet[@]}" "${quiet[@]}" autoremove --purge --yes
sudo apt-get "${quiet[@]}" "${quiet[@]}" clean --yes
apt-get "${quiet[@]}" "${quiet[@]}" autoremove --purge --yes
apt-get "${quiet[@]}" "${quiet[@]}" clean --yes
print APT cleaned.

print Clean snap...
for line in $(snap list --all | awk '/disabled/{print $1, $3}'); do
IFS=$' \t' read -r snapname revision <<< "${line}"
# shellcheck disable=SC2024
sudo snap remove "${snapname}" --purge --revision="${revision}" > "${redirection}"
snap remove "${snapname}" --purge --revision="${revision}" > "${redirection}"
done
print Snaps cleaned.

print Rotate journal...
sudo journalctl --flush --rotate --vacuum-time="${journal_retention}" "${quiet[@]}"
journalctl --flush --rotate --vacuum-time="${journal_retention}" "${quiet[@]}"
print Journal rotated.

print -n Flush 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
[[ -d /var/cache ]] && find /var/cache -mindepth 1 -delete
[[ -d /var/lib/snapd/cache ]] && find /var/lib/snapd/cache -mindepth 1 -delete
print Done.

# User caches
for user in "${users[@]}"; do
if ! homedir=$(getent passwd "${user}" | cut -d: -f6); then
echo User not exits: "${user}" >&2
exit 1
fi

print Clean-up for "${user}..."

print -n Empty trash bin...
sudo test -d "${homedir}/.local/share/Trash" && sudo find "${homedir}/.local/share/Trash" -mindepth 1 -delete
print Done.

print -n Flush Chrome cache...
sudo test -d "${homedir}/.config/google-chrome" && sudo find "${homedir}/.config/google-chrome" -mindepth 1 -delete
print Done.

print -n "Flush gdfuse (Google Drive FS) cache..."
sudo test -d "${homedir}/.gdfuse/${user}/cache" && sudo find "${homedir}/.gdfuse/${user}/cache" -mindepth 1 -delete
print Done.

print -n Flush Spotify cache...
sudo test -d "${homedir}/snap/spotify/common/.cache" && sudo find "${homedir}/snap/spotify/common/.cache" -mindepth 1 -delete
print Done.

print Cleanup finished for "${user}."
done

exit 0
6 changes: 3 additions & 3 deletions docs/bin.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,15 @@ 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 (various system & user caches)
- Flush system caches

!!! info

This script is completely standalone, no bootstrapping is required!
Perfect to run weekly by cron.

Needs root permissions.

**Usage**

```
Expand All @@ -138,8 +140,6 @@ 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
Expand Down