Skip to content

Commit 137d24d

Browse files
committed
Merge branch 'clean' into main
2 parents 9fc0b2c + 979f1b2 commit 137d24d

2 files changed

Lines changed: 17 additions & 46 deletions

File tree

bin/clean

Lines changed: 14 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
##########
1717
_version() {
1818
cat <<VERSION
19-
clean v1.3.0
19+
clean v2.0.1
2020
Written by Sandor Semsey, Copyright (C) 2023, License MIT
2121
VERSION
2222
}
@@ -28,14 +28,12 @@ _usage() {
2828
2929
Usage: clean [OPTIONS]...
3030
31-
Clean-up temporary and other unused files to free up disk space
31+
Clean-up temporary and other unused files to free up disk space. Need root permissions.
3232
3333
OPTIONS
3434
3535
-j, --journal [TIME] Remove journal older than this time (specified with the usual "s", "m", "h", etc. suffixes).
3636
Defaults to 2 weeks (2w).
37-
-u, --user [USER] Clear temp files in user's HOME also.
38-
Can be specified multiple times to clean more user HOME.
3937
-q, --quiet Suppress non-error messages
4038
-h, --help Display this help
4139
-v, --version Print version info
@@ -63,7 +61,6 @@ IFS=$'\n\t'
6361
# Parse options
6462
###############
6563
journal_retention=2w
66-
users=()
6764
quiet=()
6865
redirection=/dev/stdout
6966

@@ -73,10 +70,6 @@ while :; do
7370
shift
7471
journal_retention="${1}"
7572
;;
76-
-u | --user)
77-
shift
78-
users+=("${1}")
79-
;;
8073
-q | --quiet)
8174
quiet=(--quiet)
8275
redirection=/dev/null
@@ -100,54 +93,32 @@ while :; do
10093
shift
10194
done
10295

96+
uid=$(id -u)
97+
if [[ "${uid}" -ne 0 ]]; then
98+
echo Run as root! >&2
99+
exit 1
100+
fi
101+
103102
print Clean APT...
104-
sudo apt-get "${quiet[@]}" "${quiet[@]}" autoremove --purge --yes
105-
sudo apt-get "${quiet[@]}" "${quiet[@]}" clean --yes
103+
apt-get "${quiet[@]}" "${quiet[@]}" autoremove --purge --yes
104+
apt-get "${quiet[@]}" "${quiet[@]}" clean --yes
106105
print APT cleaned.
107106

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

116115
print Rotate journal...
117-
sudo journalctl --flush --rotate --vacuum-time="${journal_retention}" "${quiet[@]}"
116+
journalctl --flush --rotate --vacuum-time="${journal_retention}" "${quiet[@]}"
118117
print Journal rotated.
119118

120119
print -n Flush system caches...
121-
[[ -d /var/cache ]] && sudo find /var/cache -mindepth 1 -delete
122-
[[ -d /var/lib/snapd/cache ]] && sudo find /var/lib/snapd/cache -mindepth 1 -delete
120+
[[ -d /var/cache ]] && find /var/cache -mindepth 1 -delete
121+
[[ -d /var/lib/snapd/cache ]] && find /var/lib/snapd/cache -mindepth 1 -delete
123122
print Done.
124123

125-
# User caches
126-
for user in "${users[@]}"; do
127-
if ! homedir=$(getent passwd "${user}" | cut -d: -f6); then
128-
echo User not exits: "${user}" >&2
129-
exit 1
130-
fi
131-
132-
print Clean-up for "${user}..."
133-
134-
print -n Empty trash bin...
135-
sudo test -d "${homedir}/.local/share/Trash" && sudo find "${homedir}/.local/share/Trash" -mindepth 1 -delete
136-
print Done.
137-
138-
print -n Flush Chrome cache...
139-
sudo test -d "${homedir}/.config/google-chrome" && sudo find "${homedir}/.config/google-chrome" -mindepth 1 -delete
140-
print Done.
141-
142-
print -n "Flush gdfuse (Google Drive FS) cache..."
143-
sudo test -d "${homedir}/.gdfuse/${user}/cache" && sudo find "${homedir}/.gdfuse/${user}/cache" -mindepth 1 -delete
144-
print Done.
145-
146-
print -n Flush Spotify cache...
147-
sudo test -d "${homedir}/snap/spotify/common/.cache" && sudo find "${homedir}/snap/spotify/common/.cache" -mindepth 1 -delete
148-
print Done.
149-
150-
print Cleanup finished for "${user}."
151-
done
152-
153124
exit 0

docs/bin.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,13 +121,15 @@ Clean-up temporary and other not needed files to free up disk space:
121121
- Remove unused `apt` packages
122122
- Remove old snaps
123123
- Rotate system journal (keep 2 weeks of logs)
124-
- Delete caches (various system & user caches)
124+
- Flush system caches
125125

126126
!!! info
127127

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

131+
Needs root permissions.
132+
131133
**Usage**
132134

133135
```
@@ -138,8 +140,6 @@ OPTIONS
138140
-j, --journal [TIME] Remove journal older than this time
139141
specified with the usual "s", "m", "h", etc. suffixes.
140142
Defaults to 2 weeks (2w).
141-
-u, --user [USER] Clear temp files in user's HOME also.
142-
Can be specified multiple times to clean more user HOME.
143143
-q, --quiet Suppress non-error messages
144144
-h, --help Display this help
145145
-v, --version Print version info

0 commit comments

Comments
 (0)