Repository navigation
Expand file tree
/
Copy pathclean_large_caches.sh
More file actions
executable file
·123 lines (110 loc) · 3.68 KB
/
Copy pathclean_large_caches.sh
File metadata and controls
executable file
·123 lines (110 loc) · 3.68 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
#!/bin/bash
# clean_large_caches.sh — 清理快取路徑下大於 200 MB 的第一層子資料夾
# 用法:clean_large_caches.sh [--delete]
set -euo pipefail
usage() {
cat <<'EOF'
用法:clean_large_caches.sh [--delete]
預設只預覽;加上 --delete 才會永久刪除符合條件的整個子資料夾。
掃描路徑:~/.cache、/tmp、~/Library/Caches
範圍:第一層子資料夾(含隱藏資料夾),大小包含所有內容。
門檻:實際磁碟用量嚴格大於 200 MB(200,000,000 bytes)。
略過符號連結與非本人擁有的子資料夾;保留根目錄與第一層檔案。
快取根路徑若為符號連結也會略過(/tmp 除外)。
請先預覽、備份需要的資料,並關閉相關程式;/tmp 可能有使用中的資料。
請勿使用 sudo。刪除無法復原,列出的用量不保證等於可回收空間。
EOF
}
clean_root() {
local root="$1" mode="$2" min_bytes="$3"
local directory disk_usage blocks size_bytes status=0
if [[ ! -e "$root" && ! -L "$root" ]]; then
printf '略過不存在的路徑:%q\n' "$root"
return 0
fi
if [[ -L "$root" && "$root" != /tmp ]]; then
printf '略過符號連結根路徑:%q\n' "$root"
return 0
fi
if [[ ! -d "$root" || ! -r "$root" || ! -x "$root" ]]; then
printf '無法讀取資料夾:%q\n' "$root" >&2
return 1
fi
if ! root=$(cd -- "$root" && pwd -P); then
return 1
fi
if [[ "$root" == / ]]; then
printf '拒絕掃描檔案系統根目錄。\n' >&2
return 1
fi
printf '\n掃描:%q\n' "$root"
shopt -s nullglob dotglob
for directory in "$root"/*; do
[[ -d "$directory" && ! -L "$directory" ]] || continue
if [[ ! -O "$directory" ]]; then
printf '略過非本人擁有的資料夾:%q\n' "$directory"
continue
fi
# 統一 BSD/GNU du 的區塊單位;-P 不跟隨內容中的符號連結。
if ! disk_usage=$(BLOCKSIZE=512 BLOCK_SIZE=512 DU_BLOCK_SIZE=512 du -sP -- "$directory"); then
printf '無法完整計算用量,保留:%q\n' "$directory" >&2
status=1
continue
fi
blocks=${disk_usage%%$'\t'*}
if [[ ! "$blocks" =~ ^[0-9]+$ ]]; then
printf '無法解析磁碟用量,保留:%q\n' "$directory" >&2
status=1
continue
fi
size_bytes=$((blocks * 512))
(( size_bytes > min_bytes )) || continue
printf '%s bytes\t%q\n' "$size_bytes" "$directory"
if [[ "$mode" == delete ]]; then
# 掃描後再次檢查,避免直接處理已被替換的符號連結。
if [[ ! -d "$directory" || -L "$directory" || ! -O "$directory" ]]; then
printf '資料夾已變動,保留:%q\n' "$directory" >&2
status=1
continue
fi
if rm -rf -- "$directory"; then
printf '已刪除:%q\n' "$directory"
else
printf '刪除失敗(可能已刪除部分內容):%q\n' "$directory" >&2
status=1
fi
fi
done
return "$status"
}
main() {
local mode=preview root status=0
case "${1:-}" in
-h|--help) usage; return 0 ;;
--delete) mode=delete ;;
'') ;;
*) usage >&2; return 2 ;;
esac
if (( $# > 1 )); then
usage >&2
return 2
fi
if (( EUID == 0 )); then
printf '拒絕以 root 執行;請勿使用 sudo。\n' >&2
return 1
fi
if [[ "$mode" == preview ]]; then
printf '預覽模式:不會刪除任何資料。確認後加上 --delete 執行。\n'
else
printf '刪除模式:將永久刪除以下符合條件的子資料夾。\n'
fi
for root in "$HOME/.cache" /tmp "$HOME/Library/Caches"; do
if ! clean_root "$root" "$mode" 200000000; then
status=1
fi
done
return "$status"
}
if [[ "${BASH_SOURCE[0]}" == "$0" ]]; then
main "$@"
fi