Repository navigation
Expand file tree
/
Copy pathmac-clean.sh
More file actions
executable file
·92 lines (74 loc) · 2.43 KB
/
Copy pathmac-clean.sh
File metadata and controls
executable file
·92 lines (74 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/bin/bash
# Author: Miguel Barra <mbarra.git@gmail.com>
set -uo pipefail
LOG_DIR="$HOME/Library/Logs/NightlyCleanup"
LOG_FILE="$LOG_DIR/cleanup_$(date +%Y-%m-%d_%H-%M-%S).log"
mkdir -p "$LOG_DIR"
log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" | tee -a "$LOG_FILE"
}
safe_clear_dir_contents() {
local target="$1"
local exclude_pattern="${2:-}"
if [ -d "$target" ]; then
find "$target" -mindepth 1 -maxdepth 1 2>/dev/null | while read -r item; do
base_lower=$(basename "$item" | tr '[:upper:]' '[:lower:]')
if [ -n "$exclude_pattern" ] && [[ "$base_lower" == *"$exclude_pattern"* ]]; then
continue
fi
rm -rf "$item" 2>/dev/null
done
fi
}
log "Starting safe nightly system cleanup"
BEFORE_FREE=$(df -h / | awk 'NR==2{print $4}')
log "Free space before: $BEFORE_FREE"
log "Cleaning user caches (excluding Photos-related items)"
if [ -d "$HOME/Library/Caches" ]; then
find "$HOME/Library/Caches" -mindepth 1 -maxdepth 1 2>/dev/null | while read -r item; do
base_lower=$(basename "$item" | tr '[:upper:]' '[:lower:]')
if [[ "$base_lower" == *"photo"* ]]; then
continue
fi
rm -rf "$item" 2>/dev/null
done
fi
log "Cleaning user logs"
safe_clear_dir_contents "$HOME/Library/Logs" "nightlycleanup"
log "Cleaning crash reports"
safe_clear_dir_contents "$HOME/Library/Application Support/CrashReporter"
log "Cleaning user temporary files"
if [ -n "${TMPDIR:-}" ] && [ -d "$TMPDIR" ]; then
find "$TMPDIR" -mindepth 1 -maxdepth 1 2>/dev/null -exec rm -rf {} + 2>/dev/null
fi
log "Cleaning QuickLook cache"
qlmanage -r cache >/dev/null 2>&1
log "Restarting QuickLook service"
qlmanage -r >/dev/null 2>&1
if command -v brew >/dev/null 2>&1; then
log "Cleaning Homebrew cache"
brew cleanup -s --prune=all >>"$LOG_FILE" 2>&1
rm -rf "$(brew --cache)" 2>/dev/null
fi
if command -v npm >/dev/null 2>&1; then
log "Cleaning npm cache"
npm cache clean --force >>"$LOG_FILE" 2>&1
fi
if command -v yarn >/dev/null 2>&1; then
log "Cleaning yarn cache"
yarn cache clean >>"$LOG_FILE" 2>&1
fi
if command -v pip3 >/dev/null 2>&1; then
log "Cleaning pip cache"
pip3 cache purge >>"$LOG_FILE" 2>&1
fi
if command -v docker >/dev/null 2>&1; then
if docker info >/dev/null 2>&1; then
log "Cleaning unused Docker resources"
docker system prune -f >>"$LOG_FILE" 2>&1
fi
fi
AFTER_FREE=$(df -h / | awk 'NR==2{print $4}')
log "Free space after: $AFTER_FREE"
log "Nightly cleanup completed"
exit 0