Repository navigation
Expand file tree
/
Copy pathdisk-cleanup
More file actions
executable file
·199 lines (167 loc) · 6.05 KB
/
Copy pathdisk-cleanup
File metadata and controls
executable file
·199 lines (167 loc) · 6.05 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/bin/bash
# Disk Space Cleanup Script
# Modular cleanup orchestrator that runs various cleanup commands
set -e
# Configuration
LOG_FILE="$HOME/.notes/cleanup.log"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
MODULES_DIR="$SCRIPT_DIR/cleanup-modules"
# Ensure log directory exists
mkdir -p "$(dirname "$LOG_FILE")"
# Parse command line arguments
MODE="conservative"
SPECIFIC_MODULE=""
DRY_RUN=false
INCLUDE_CARGO=false
show_usage() {
cat <<EOF
Usage: disk-cleanup [OPTIONS]
Options:
--aggressive Run cleanup in aggressive mode (removes more data)
--include-cargo Also clean shared CARGO_TARGET_DIR (large but slow to rebuild)
--module MODULE Run only the specified module (homebrew, docker, node, python, rust)
--dry-run Show what would be cleaned without actually cleaning
--help Show this help message
Examples:
disk-cleanup # Run all modules in conservative mode
disk-cleanup --aggressive # Run all modules in aggressive mode
disk-cleanup --module rust # Only run Rust cleanup
disk-cleanup --module rust --aggressive # Aggressive Rust cleanup only
disk-cleanup --dry-run # Preview what would be cleaned
Available modules:
homebrew - Homebrew packages and cache
docker - Docker containers, images, networks, volumes
node - npm, yarn, pnpm caches
python - pip, UV, pytest, puppeteer caches
rust - Rust target directories and cargo cache
flox - Flox logs across all projects
timemachine - Time Machine local snapshots
EOF
}
while [[ $# -gt 0 ]]; do
case $1 in
--aggressive)
MODE="aggressive"
shift
;;
--module)
SPECIFIC_MODULE="$2"
shift 2
;;
--include-cargo)
INCLUDE_CARGO=true
shift
;;
--dry-run)
DRY_RUN=true
shift
;;
--help|-h)
show_usage
exit 0
;;
*)
echo "Unknown option: $1"
show_usage
exit 1
;;
esac
done
# Function to log message
log_message() {
echo "$(date '+%Y-%m-%d %H:%M:%S'): $1" | tee -a "$LOG_FILE"
}
# Function to run command with logging
run_cleanup() {
local description="$1"
local command="$2"
log_message "Starting: $description"
if [ "$DRY_RUN" = true ]; then
log_message "[DRY RUN] Would run: $command"
return 0
fi
if eval "$command"; then
log_message "Completed: $description"
else
log_message "Failed: $description"
fi
echo
}
# Export functions and variables for modules to use
export -f log_message
export -f run_cleanup
export LOG_FILE
export DRY_RUN
export INCLUDE_CARGO
# Capture disk space before cleanup (in KB for accurate calculations)
if [ "$DRY_RUN" = false ]; then
SPACE_BEFORE=$(df -k /System/Volumes/Data | tail -1 | awk '{print $3}')
fi
# Start cleanup
log_message "Starting disk cleanup (mode: $MODE)..."
if [ "$DRY_RUN" = true ]; then
log_message "DRY RUN MODE - No actual changes will be made"
fi
# Function to run a cleanup module
run_module() {
local module_name="$1"
local module_file="$MODULES_DIR/${module_name}.sh"
if [ -f "$module_file" ]; then
log_message "Running module: $module_name"
# shellcheck source=/dev/null
source "$module_file"
"cleanup_${module_name}" "$MODE"
else
log_message "Module not found: $module_name"
fi
}
# Run specific module or all modules
if [ -n "$SPECIFIC_MODULE" ]; then
run_module "$SPECIFIC_MODULE"
else
# Run all modules in order
run_module "homebrew"
run_module "node"
run_module "docker"
run_module "python"
run_module "rust"
# Flox logs cleanup (not in a module yet - inline for now)
log_message "Running Flox logs cleanup..."
if [ -d "$HOME/dev/posthog/posthog/.flox/log" ]; then
run_cleanup "Flox logs cleanup" "rm -rf ~/dev/posthog/posthog/.flox/log/*"
fi
# General flox logs cleanup in all projects
find "$HOME/dev" -name ".flox" -type d 2>/dev/null | while read -r flox_dir; do
if [ -d "$flox_dir/log" ] && [ "$(find "$flox_dir/log" -type f | wc -l)" -gt 0 ]; then
run_cleanup "Flox logs cleanup in $(dirname "$flox_dir")" "rm -rf '$flox_dir/log'/*"
fi
done
# Nix store garbage collection (removes unused packages from Flox/Nix)
if command -v nix-collect-garbage >/dev/null 2>&1; then
run_cleanup "Nix store garbage collection" "nix-collect-garbage -d"
fi
# Time Machine local snapshots cleanup
log_message "Running Time Machine cleanup..."
run_cleanup "Remove Time Machine local snapshots" "sudo tmutil deletelocalsnapshots /"
fi
log_message "Disk cleanup completed"
# Show disk usage and reclaimed space after cleanup
if [ "$DRY_RUN" = false ]; then
# Capture disk space after cleanup
SPACE_AFTER=$(df -k /System/Volumes/Data | tail -1 | awk '{print $3}')
# Calculate space reclaimed (before - after = freed space)
SPACE_RECLAIMED=$((SPACE_BEFORE - SPACE_AFTER))
# Convert to human readable format
if [ "$SPACE_RECLAIMED" -gt 1048576 ]; then
RECLAIMED_DISPLAY=$(echo "scale=2; $SPACE_RECLAIMED / 1048576" | bc)" GB"
elif [ "$SPACE_RECLAIMED" -gt 1024 ]; then
RECLAIMED_DISPLAY=$(echo "scale=2; $SPACE_RECLAIMED / 1024" | bc)" MB"
else
RECLAIMED_DISPLAY="${SPACE_RECLAIMED} KB"
fi
echo
echo "═══════════════════════════════════════════════════════════"
echo "Total space reclaimed: $RECLAIMED_DISPLAY"
echo "Free space available: $(df -h /System/Volumes/Data | tail -1 | awk '{print $4}')"
echo "═══════════════════════════════════════════════════════════"
fi