-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhousekeeping
More file actions
executable file
·208 lines (191 loc) · 4.72 KB
/
Copy pathhousekeeping
File metadata and controls
executable file
·208 lines (191 loc) · 4.72 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
#!/bin/bash
declare -a task_array
arguments="$@"
if [[ "$#" -lt 1 ]]; then
echo "Select tasks to perform:"
echo -e "\t1. Update and upgrade apt-packages\t[u]"
echo -e "\t2. Remove obsolete apt-packages\t\t[r]"
echo -e "\t3. Remove obsolete snap files\t\t[s]"
read -p 'Enter one or more options [u|r|s]: ' arguments
fi
if [[ "$arguments" =~ (s|snap) ]]; then
task_array+=('snap_main')
echo "added snap_main"
fi
if [[ "$arguments" =~ (u|update) ]]; then
task_array+=('update_apt')
echo "added update_apt"
fi
if [[ "$arguments" =~ (r|remove) ]]; then
task_array+=('clean_apt')
echo "added clean_apt"
fi
for task in "${task_array[@]}"; do
echo "$task"
done
update_apt(){
echo "Software Update"
echo "Looking for updates to installed packages..."
sudo apt update
echo "Upgrading packages..."
sudo apt upgrade -y
echo "apt update complete."
}
clean_apt(){
echo "APT Auto Clean"
echo "Finding and removing obsolete packages..."
sudo apt-get autoremove -y
echo "Finding and removing archive files..."
sudo apt-get autoclean -y
echo "apt autoclean and autoremove complete."
}
get_index(){
#takes array as first arg, value as second arg
local -n array=$1
for ind in "${!array[@]}"; do
if [[ "${array[$ind]}" = $2 ]]; then
echo "$ind"
fi
done
}
clear_snapd(){
if [[ "$#" -ge 1 ]]; then
force="$1"
else
force=""
fi
echo "Snap Cleaner"
echo "Found the following obsolete snaps:"
sudo ls -holA /var/lib/snapd/snaps/ | awk '{print $8}' | awk -F'[_.]' '{print $1}' | uniq -D | uniq
if [[ "force" = "f" ]]; then
snapd_choice="y"
else
echo "Would you like to delete these files?"
read -p "[yes(y), no(n), quit(q): " snapd_choice
fi
case $snapd_choice in
yes|y)
while read -r snapcount; do
echo "$snapcount has multiple versions"
snaparray[$snapcount]=0
readarray -t versions < <(sudo ls -holA /var/lib/snapd/snaps/ | grep "$snapcount" | awk '{print $8}' | awk -F'[_.]' '{printf "%s\n", $2}' | awk '!/^[[:space:]]*$/')
# for bla in "${versions[@]}"; do
# echo -e "\t$bla"
# done
IFS=$'\n'
highest_value=$(echo "${versions[*]}" | sort -nr | head -n1)
echo "the most recent version is: $highest_value"
highest_value_index=$(get_index versions $highest_value)
# echo "the index for $highest_value is: $highest_value_index"
echo "these versions are to be deleted: ${versions[@]/$highest_value}"
# echo "deleting highest value from versions array"
unset versions[$highest_value_index]
# echo "versions array now contains:"
# echo "${versions[*]}"
for tbd in "${versions[@]}"; do
full_name="${snapcount}_${tbd}.snap"
echo "deleting $full_name"
sudo rm /var/lib/snapd/snaps/$full_name
done
done < <(sudo ls -holA /var/lib/snapd/snaps/ | awk '{print $8}' | awk -F'[_.]' '{print $1}' | uniq -D | uniq)
;;
no|n)
echo "Files left unchanged"
;;
quit|q)
exit 0
;;
*)
echo "input not recognised, returning to main menu"
main
;;
esac
}
clear_disabled(){
set -eu
if [[ "$#" -ge 1 ]]; then
force="$1"
else
force=""
fi
echo "Found the following disabled snaps:"
snap list --all | awk '/disabled/{print $1, $3}'
if [[ "$force" = "f" ]]; then
disabled_choice="y"
else
echo "do you wish to delete these snaps?"
read -p "[yes(y), no(n), quit(q): " disabled_choice
fi
case $disabled_choice in
yes|y)
snap list --all | awk '/disabled/{print $1, $3}' |
while read snapname revision; do
snap remove "$snapname" --revision="$revision"
echo "removing $snapname ($revision)"
done
echo "successfully deleted old snaps, returning to main menu"
;;
no|n)
echo "leaving snaps unchanged, returning to main menu"
;;
quit|q)
echo "exiting..."
exit 0
;;
*)
echo "input not recognised, returning to main menu"
;;
esac
}
snap_main(){
echo ">>>>>>>>>>>>>>> SNAP cleaner <<<<<<<<<<<<<<<<"
echo ""
# if [[ "$1" = "-f" ]]; then
# main_choice="4"
# else
echo "What do you want do do?"
echo -e "\t1. Clean disabled snaps"
echo -e "\t2. Remove old versions in /var/lib/snapd/snaps/"
echo -e "\t3. Both"
echo -e "\t4. Both without asking for confirmation"
echo -e "\t5. Exit"
read -p "Enter choice [1, 2, 3, 4, 5]: " main_choice
# fi
case $main_choice in
1)
clear_disabled
echo "Done, returning to main menu"
snap_main
;;
2)
clear_snapd
echo "Done, returning to main menu"
snap_main
;;
3)
clear_disabled
clear_snapd
echo "Done, returning to main menu"
snap_main
;;
4)
echo "Deleting obsolete snaps..."
clear_disabled f
clear_snapd f
echo "Done"
;;
5)
echo "Exiting..."
;;
esac
}
main(){
echo "Performing selected tasks..."
for task in "${task_array[@]}"; do
echo "calling $task"
$task
done
echo "All done!"
}
main $@
exit 0