-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclean.sh
executable file
·129 lines (116 loc) · 3.79 KB
/
clean.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
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
#!/usr/bin/bash
path=(/home/* /var)
clear_log(){
if [ $(du -s ${path[0]}/clean.log | awk '{print $1}') -gt 12 ];then
> ${path[0]}/clean.log
fi
}
convert_to_GB(){
echo $1 | awk '{$1=$1/(1024^2); printf "%.2f %s\n", $1,"GB";}'
}
add_to_log(){
echo "$1: $2" >> ${path[0]}/clean.log
}
calculate_GB(){
fin=$(convert_to_GB $1)
}
calculate_total(){
total=$((total + cache))
}
clear_cache(){
if [ "$1" != "Edge cache" ];then
cache=$(du -s $2 | awk '{print $1}')
else
cache=$(($(du -s $2 | awk '{print $1}') + $(du -s "$3" | awk '{print $1}') + $(du -s "$4" | awk '{print $1}') + $(du -c $5/*.blob | awk '/total/ {print $1}') + $(du -s $6 | awk '{print $1}')))
fi
calculate_GB $cache
if [ "$fin" != "0.00 GB" ];then
calculate_total
case $1 in
"DNF cache")
dnf clean all > /dev/null
;;
"Edge cache")
rm -r $2/* "$3"/* "$4"/*
rm -rf $5/*.blob
sudo find $6/* -maxdepth 0 -type f -not -iname "en-US.pak" -exec rm -r {} \;
;;
*)
rm -rf $2/*
;;
esac
echo -e "Deleted \033[0;35m$1: \033[0m"$fin
add_to_log "$1" "$fin"
fi
}
clear_nvidia(){
nvidia=(nsight-compute nsight-systems)
IFS=$'\n' ## only word-split on '\n'
for i in "${nvidia[@]}"
do
x=$(ls -l "$3/$i" | grep -c ^d)
if [ $x -gt 1 ];then
readarray a < <(ls "$3/$i")
echo -e "Total folders in \033[0;35m$i\033[0m: \033[0;36m${#a[@]}\033[0m"
a=( $(printf "%s\n" ${a[@]} | sort -r ) ) ## reverse sort
echo -e "Keeping \033[0;32m$a\033[0m"
unset a[0]
cache=0
j="0"
for j in "${a[@]}"
do
nvidia_cache=$(du -s $3/$i/$j | awk '{print $1}')
cache=$(($cache + $nvidia_cache))
sudo rm -rf "$3/$i/$j"
echo -e "Deleted \033[0;31m$j: \033[0m"$(convert_to_GB $nvidia_cache $i)
done
add_to_log "$1-$i" $(convert_to_GB $cache)
calculate_total
fi
done
x=$(ls "$4" | grep "$2-" | wc -l)
if [ $x -gt 2 ];then
readarray a < <(ls $4 | grep "$2-[1-9][1-9]$" | sort -r)
readarray b < <(ls $4 | grep "$2-[1-9][1-9].[1-9]$" | sort -r)
echo -e "Total folders of major versions \033[0;35m$2-xx\033[0m: \033[0;36m${#a[@]}\033[0m"
echo -e "Total folders of minor versions \033[0;35m$2-xx.x\033[0m: \033[0;36m${#b[@]}\033[0m"
a=( $(printf "%s\n" ${a[@]}) )
b=( $(printf "%s\n" ${b[@]}) )
if [ ${#a[@]} -gt 1 ];then
echo -e "Keeping \033[0;32m$a\033[0m"
unset a[0]
for i in "${a[@]}"
do
sudo unlink "$4/$i"
echo -e "Deleted \033[0;31m$i\033[0m"
done
fi
if [ ${#b[@]} -gt 1 ];then
echo -e "Keeping \033[0;32m$b\033[0m"
unset b[0]
cache=0
for i in "${b[@]}"
do
cuda_cache=$(du -s $4/$i | awk '{print $1}')
cache=$(($cache + $cuda_cache))
sudo rm -rf "$4/$i"
echo -e "Deleted \033[0;31m$i: \033[0m"$(convert_to_GB $cuda_cache)
done
add_to_log "$1-$2" $(convert_to_GB $cache)
calculate_total
fi
fi
}
clear_log
echo "-----$(date +'%d/%m/%y %r')-----" >> ${path[0]}/clean.log
clear_cache "Thumbnails cache" "${path[0]}/.cache/thumbnails/x-large"
clear_cache "Pip cache" "${path[0]}/.cache/pip"
clear_cache "Edge cache" "${path[0]}/.cache/microsoft-edge/Default/Cache/Cache_Data" "${path[0]}/.cache/microsoft-edge/Default/Code Cache/js" "${path[0]}/.config/microsoft-edge/Default/Service Worker/CacheStorage" "${path[0]}/.config/microsoft-edge/Default/IndexedDB" "/opt/microsoft/msedge/locales"
clear_cache "Firefox cache" "${path[0]}/.cache/mozilla/firefox/$(ls ${path[0]}/.cache/mozilla/firefox)/cache2/entries"
clear_cache "DNF cache" "${path[1]}/cache/libdnf5"
clear_cache "Coredumps" "${path[1]}/lib/systemd/coredump"
clear_cache "Journal logs" "${path[1]}/log/journal"
clear_nvidia "Nvidia" "cuda" "/opt/nvidia" "/usr/local"
total=$(convert_to_GB $total)
add_to_log "Total storage recovered" "$total"
echo -e "\nTotal storage recovered: \033[0;32m$total\033[0m"