-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsystem_cleanup.sh
executable file
·103 lines (85 loc) · 2.24 KB
/
system_cleanup.sh
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
#!/bin/sh
# path: /home/klassiker/.local/share/repos/shell/system_cleanup.sh
# author: klassiker [mrdotx]
# github: https://github.com/mrdotx/shell
# date: 2023-12-12T09:18:09+0100
# helper
find_files() {
cache_directory="$1"
cache_days="$2"
shift 2
find "$cache_directory" \
-type f \
-mtime +"$cache_days" \
-not -path "*/paru/*" \
-not -name "wallpaper.jpg" \
"$@"
}
cleanup_file() {
! [ -f "$1" ] \
&& return 0
printf ":: cleanup file \"%s\"\n" \
"$1"
printf " remove white space from the end of the line...\n"
sed -i 's/ *$//' "$1"
printf " remove duplicates...\n"
printf "%s\n" "$(tac "$1" \
| awk '! seen[$0]++' \
| tac \
)" > "$1"
}
delete_files() {
! [ -d "$1" ] \
&& return 0
cache_files=$(find_files "$1" "$2" \
| wc -l \
)
[ "$cache_files" -gt 0 ] \
|| return 0
printf "\n:: delete %d files from \"%s\" not accessed in %d days...\n" \
"$cache_files" \
"$1" \
"$2"
find_files "$1" "$2"
printf " delete files from \"%s\" [y]es/[N]o: " \
"$1" \
&& read -r key
case "$key" in
y|Y|yes|Yes)
find_files "$1" "$2" -delete
;;
esac
}
delete_pkgs() {
! [ -d "$1" ] \
&& return 0
auth="${EXEC_AS_USER:-sudo}"
dry_run=$($auth find "$1" -type d \
-exec printf ":: delete pkgs from \"{}\", except last %s versions\n" \
"$2" \; \
-exec paccache -dvk "$2" -c {} \; \
)
printf "%s\n" \
"$dry_run" \
| grep -q "Candidate packages:" \
|| return 0
printf "\n%s\n" \
"$dry_run"
printf " delete pkgs from \"%s\" [y]es/[N]o: " \
"$1" \
&& read -r clear_pkgs \
&& case "$clear_pkgs" in
y|Y|yes|Yes)
$auth find "$1" -type d \
-exec paccache -rvk "$2" -c {} \;
;;
esac
}
# main
cleanup_file "$HOME/.local/share/iwctl/history"
printf "\n"
cleanup_file "$HOME/.local/share/cmd_history"
delete_files "$HOME/.cache" 365
delete_pkgs "/srv/pacman/core/os/x86_64" 2
delete_pkgs "/srv/pacman/extra/os/x86_64" 2
delete_pkgs "/srv/aurutils/aurbuild" 2