-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcleanubuntu
More file actions
513 lines (431 loc) · 13.3 KB
/
cleanubuntu
File metadata and controls
513 lines (431 loc) · 13.3 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
#!/bin/bash
# CleanUbuntu - Comprehensive system cleanup utility
# Version: 1.0.0
set -e
VERSION="1.0.0"
SCRIPT_NAME="cleanubuntu"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
NC='\033[0m' # No Color
# Print colored output
print_success() { echo -e "${GREEN}✓${NC} $1"; }
print_error() { echo -e "${RED}✗${NC} $1"; }
print_info() { echo -e "${BLUE}ℹ${NC} $1"; }
print_warning() { echo -e "${YELLOW}⚠${NC} $1"; }
print_header() { echo -e "${CYAN}${1}${NC}"; }
# Check if running as root
check_root() {
if [ "$EUID" -ne 0 ]; then
print_error "This script must be run with sudo"
echo "Usage: sudo $SCRIPT_NAME [OPTIONS]"
exit 1
fi
}
# Get actual user (when using sudo)
ACTUAL_USER="${SUDO_USER:-$USER}"
ACTUAL_HOME=$(eval echo ~$ACTUAL_USER)
# Calculate size of directory
get_size() {
du -sh "$1" 2>/dev/null | cut -f1 || echo "0"
}
# Show usage
show_usage() {
cat << EOF
${BLUE}CleanUbuntu${NC} - Comprehensive system cleanup utility v${VERSION}
${YELLOW}Usage:${NC}
sudo $SCRIPT_NAME [OPTIONS]
${YELLOW}Options:${NC}
-a, --all Run all cleanup operations (safe mode)
-q, --quick Quick cleanup (apt cache + trash)
-d, --deep Deep cleanup (includes logs, cache, thumbnails)
--apt Clean APT cache and old packages
--snap Clean old snap versions
--flatpak Clean unused Flatpak data
--kernels Remove old kernels (keep current + 1)
--logs Clean system and journal logs
--cache Clean user cache directories
--trash Empty trash for all users
--thumbnails Clean thumbnail cache
--temp Clean temporary files
--browser Clean browser caches
--analyze Show what would be cleaned (dry run)
-y, --yes Skip confirmation prompts
-h, --help Show this help message
-v, --version Show version information
${YELLOW}Examples:${NC}
sudo $SCRIPT_NAME --analyze
→ Show potential space savings without cleaning
sudo $SCRIPT_NAME --quick
→ Quick cleanup of APT cache and trash
sudo $SCRIPT_NAME --all
→ Run all safe cleanup operations
sudo $SCRIPT_NAME --deep -y
→ Deep cleanup without confirmations
sudo $SCRIPT_NAME --apt --snap --kernels
→ Clean specific items only
${YELLOW}Note:${NC}
Always run with 'sudo' for full system access
Use --analyze first to see what will be cleaned
EOF
}
# Show version
show_version() {
echo "$SCRIPT_NAME version $VERSION"
}
# Analyze space usage
analyze_space() {
print_header "=== SPACE ANALYSIS ==="
echo ""
# APT cache
if [ -d "/var/cache/apt/archives" ]; then
local apt_size=$(get_size "/var/cache/apt/archives")
echo "APT cache: $apt_size"
fi
# Snap
if command -v snap &> /dev/null; then
local snap_disabled=$(snap list --all 2>/dev/null | awk '/disabled/{print $1, $3}' | wc -l)
if [ "$snap_disabled" -gt 0 ]; then
echo "Old snap versions: ~$((snap_disabled * 50))MB (estimated)"
fi
fi
# Journal logs
if command -v journalctl &> /dev/null; then
local journal_size=$(journalctl --disk-usage 2>/dev/null | grep -oP '\d+\.\d+[GM]' || echo "unknown")
echo "Journal logs: $journal_size"
fi
# System logs
if [ -d "/var/log" ]; then
local log_size=$(get_size "/var/log")
echo "System logs: $log_size"
fi
# User cache
if [ -d "$ACTUAL_HOME/.cache" ]; then
local cache_size=$(get_size "$ACTUAL_HOME/.cache")
echo "User cache: $cache_size"
fi
# Trash
if [ -d "$ACTUAL_HOME/.local/share/Trash" ]; then
local trash_size=$(get_size "$ACTUAL_HOME/.local/share/Trash")
echo "Trash: $trash_size"
fi
# Thumbnails
if [ -d "$ACTUAL_HOME/.cache/thumbnails" ]; then
local thumb_size=$(get_size "$ACTUAL_HOME/.cache/thumbnails")
echo "Thumbnails: $thumb_size"
fi
# Temp files
if [ -d "/tmp" ]; then
local tmp_size=$(get_size "/tmp")
echo "Temporary files: $tmp_size"
fi
echo ""
print_info "Run with --all to clean all items"
print_info "Run with --deep for more aggressive cleaning"
}
# Clean APT cache
clean_apt() {
print_header "Cleaning APT cache..."
apt-get clean -y 2>/dev/null
apt-get autoclean -y 2>/dev/null
apt-get autoremove -y 2>/dev/null
print_success "APT cache cleaned"
}
# Clean old snap versions
clean_snap() {
if ! command -v snap &> /dev/null; then
print_warning "Snap not installed, skipping"
return
fi
print_header "Cleaning old snap versions..."
LANG=C snap list --all 2>/dev/null | awk '/disabled/{print $1, $3}' | while read snapname revision; do
snap remove "$snapname" --revision="$revision" 2>/dev/null || true
done
print_success "Old snap versions removed"
}
# Clean flatpak
clean_flatpak() {
if ! command -v flatpak &> /dev/null; then
print_warning "Flatpak not installed, skipping"
return
fi
print_header "Cleaning Flatpak..."
flatpak uninstall --unused -y 2>/dev/null || true
print_success "Unused Flatpak data removed"
}
# Remove old kernels
clean_kernels() {
print_header "Removing old kernels..."
local current_kernel=$(uname -r)
print_info "Current kernel: $current_kernel"
local old_kernels=$(dpkg --list 2>/dev/null | grep -E 'linux-image-[0-9]' | awk '{print $2}' | grep -v "$current_kernel" | head -n -1)
if [ -z "$old_kernels" ]; then
print_info "No old kernels to remove"
return
fi
echo "$old_kernels" | while read kernel; do
print_info "Removing $kernel..."
apt-get remove --purge -y "$kernel" 2>/dev/null || true
done
print_success "Old kernels removed (kept current + 1 previous)"
}
# Clean system logs
clean_logs() {
print_header "Cleaning system logs..."
# Clean journal logs (keep last 3 days)
if command -v journalctl &> /dev/null; then
journalctl --vacuum-time=3d 2>/dev/null || true
print_success "Journal logs cleaned (kept last 3 days)"
fi
# Clean old log files
find /var/log -type f -name "*.log.*" -delete 2>/dev/null || true
find /var/log -type f -name "*.gz" -delete 2>/dev/null || true
# Truncate large log files
find /var/log -type f -name "*.log" -size +50M -exec truncate -s 0 {} \; 2>/dev/null || true
print_success "System logs cleaned"
}
# Clean user cache
clean_cache() {
print_header "Cleaning user cache..."
if [ -d "$ACTUAL_HOME/.cache" ]; then
# Don't delete entire cache, just old/large items
find "$ACTUAL_HOME/.cache" -type f -atime +30 -delete 2>/dev/null || true
print_success "User cache cleaned (removed files older than 30 days)"
else
print_info "No cache directory found"
fi
}
# Empty trash
clean_trash() {
print_header "Emptying trash..."
# Empty trash for the actual user
if [ -d "$ACTUAL_HOME/.local/share/Trash" ]; then
rm -rf "$ACTUAL_HOME/.local/share/Trash"/* 2>/dev/null || true
print_success "Trash emptied for $ACTUAL_USER"
fi
# Empty trash for all users (if run as root)
for user_home in /home/*; do
if [ -d "$user_home/.local/share/Trash" ]; then
local username=$(basename "$user_home")
rm -rf "$user_home/.local/share/Trash"/* 2>/dev/null || true
print_success "Trash emptied for $username"
fi
done
}
# Clean thumbnails
clean_thumbnails() {
print_header "Cleaning thumbnails..."
if [ -d "$ACTUAL_HOME/.cache/thumbnails" ]; then
rm -rf "$ACTUAL_HOME/.cache/thumbnails"/* 2>/dev/null || true
print_success "Thumbnail cache cleared"
fi
# Clean for all users
for user_home in /home/*; do
if [ -d "$user_home/.cache/thumbnails" ]; then
rm -rf "$user_home/.cache/thumbnails"/* 2>/dev/null || true
fi
done
}
# Clean temp files
clean_temp() {
print_header "Cleaning temporary files..."
# Clean /tmp (but preserve structure)
find /tmp -type f -atime +7 -delete 2>/dev/null || true
find /tmp -type d -empty -delete 2>/dev/null || true
# Clean /var/tmp
find /var/tmp -type f -atime +7 -delete 2>/dev/null || true
print_success "Temporary files cleaned"
}
# Clean browser caches
clean_browser() {
print_header "Cleaning browser caches..."
# Firefox
if [ -d "$ACTUAL_HOME/.cache/mozilla" ]; then
rm -rf "$ACTUAL_HOME/.cache/mozilla"/* 2>/dev/null || true
print_success "Firefox cache cleared"
fi
# Chrome/Chromium
if [ -d "$ACTUAL_HOME/.cache/google-chrome" ]; then
rm -rf "$ACTUAL_HOME/.cache/google-chrome"/* 2>/dev/null || true
print_success "Chrome cache cleared"
fi
if [ -d "$ACTUAL_HOME/.cache/chromium" ]; then
rm -rf "$ACTUAL_HOME/.cache/chromium"/* 2>/dev/null || true
print_success "Chromium cache cleared"
fi
}
# Parse arguments
ANALYZE_ONLY=false
QUICK=false
DEEP=false
ALL=false
DO_APT=false
DO_SNAP=false
DO_FLATPAK=false
DO_KERNELS=false
DO_LOGS=false
DO_CACHE=false
DO_TRASH=false
DO_THUMBNAILS=false
DO_TEMP=false
DO_BROWSER=false
SKIP_CONFIRM=false
if [ $# -eq 0 ]; then
show_usage
exit 0
fi
while [[ $# -gt 0 ]]; do
case $1 in
-a|--all)
ALL=true
shift
;;
-q|--quick)
QUICK=true
shift
;;
-d|--deep)
DEEP=true
shift
;;
--analyze)
ANALYZE_ONLY=true
shift
;;
--apt)
DO_APT=true
shift
;;
--snap)
DO_SNAP=true
shift
;;
--flatpak)
DO_FLATPAK=true
shift
;;
--kernels)
DO_KERNELS=true
shift
;;
--logs)
DO_LOGS=true
shift
;;
--cache)
DO_CACHE=true
shift
;;
--trash)
DO_TRASH=true
shift
;;
--thumbnails)
DO_THUMBNAILS=true
shift
;;
--temp)
DO_TEMP=true
shift
;;
--browser)
DO_BROWSER=true
shift
;;
-y|--yes)
SKIP_CONFIRM=true
shift
;;
-h|--help)
show_usage
exit 0
;;
-v|--version)
show_version
exit 0
;;
*)
print_error "Unknown option: $1"
echo "Use -h or --help for usage information"
exit 1
;;
esac
done
# Handle analyze mode
if [ "$ANALYZE_ONLY" = true ]; then
analyze_space
exit 0
fi
# Check for root
check_root
# Set flags based on modes
if [ "$ALL" = true ]; then
DO_APT=true
DO_SNAP=true
DO_FLATPAK=true
DO_LOGS=true
DO_CACHE=true
DO_TRASH=true
DO_THUMBNAILS=true
DO_TEMP=true
fi
if [ "$QUICK" = true ]; then
DO_APT=true
DO_TRASH=true
fi
if [ "$DEEP" = true ]; then
DO_APT=true
DO_SNAP=true
DO_FLATPAK=true
DO_KERNELS=true
DO_LOGS=true
DO_CACHE=true
DO_TRASH=true
DO_THUMBNAILS=true
DO_TEMP=true
DO_BROWSER=true
fi
# Confirmation prompt
if [ "$SKIP_CONFIRM" = false ]; then
echo ""
print_warning "This will clean various system files and caches"
print_info "Use --analyze to see what will be cleaned first"
echo ""
read -p "Continue? (y/N) " -n 1 -r
echo ""
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
print_info "Cancelled"
exit 0
fi
fi
# Get initial disk space
SPACE_BEFORE=$(df / | awk 'NR==2 {print $3}')
echo ""
print_header "╔════════════════════════════════════════╗"
print_header "║ Starting CleanUbuntu v$VERSION ║"
print_header "╚════════════════════════════════════════╝"
echo ""
# Run selected operations
[ "$DO_APT" = true ] && clean_apt
[ "$DO_SNAP" = true ] && clean_snap
[ "$DO_FLATPAK" = true ] && clean_flatpak
[ "$DO_KERNELS" = true ] && clean_kernels
[ "$DO_LOGS" = true ] && clean_logs
[ "$DO_CACHE" = true ] && clean_cache
[ "$DO_TRASH" = true ] && clean_trash
[ "$DO_THUMBNAILS" = true ] && clean_thumbnails
[ "$DO_TEMP" = true ] && clean_temp
[ "$DO_BROWSER" = true ] && clean_browser
# Calculate space freed
SPACE_AFTER=$(df / | awk 'NR==2 {print $3}')
SPACE_FREED=$((SPACE_BEFORE - SPACE_AFTER))
echo ""
print_header "╔════════════════════════════════════════╗"
print_header "║ Cleanup Complete! ║"
print_header "╚════════════════════════════════════════╝"
echo ""
print_success "Space freed: ~$((SPACE_FREED / 1024))MB"
echo ""