-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsystemCleaning
More file actions
executable file
·136 lines (101 loc) · 3.71 KB
/
systemCleaning
File metadata and controls
executable file
·136 lines (101 loc) · 3.71 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
#!/usr/bin/env bash
# Package cache cleaning implementation from @albertored11
RED='\033[0;31m'
GREEN='\033[0;32m'
BLUE='\033[0;34m'
NC='\033[0m'
if [ $EUID = 0 ]; then
USER_HOME=$(getent passwd "$SUDO_USER" | cut -d: -f6)
else
USER_HOME=$HOME
echo -e "\n${RED}Some operations require root permission!${NC}"
fi
if [[ $1 == '' ]]; then
DEST_FOLDER=$(pwd)
else
DEST_FOLDER=$1
fi
DEST_FILE="orphanedPackages.txt"
cachedir="$USER_HOME/.cache/yay"
removed="$(comm -23 <(basename -a $(find $cachedir -mindepth 1 -maxdepth 1 -type d) | sort) <(pacman -Qqm) | xargs -r printf "$cachedir/%s\n")"
echo -e "\n${BLUE}Creating $DEST_FOLDER if it doesn't exist${NC}"
mkdir -pv "$DEST_FOLDER"
chown "$SUDO_USER":"$SUDO_USER" "$DEST_FOLDER"
echo -e "${BLUE}"
read -n 1 -rp "Clean journal? [Y/n] " prompt
# Convert input to lowercase
prompt=${prompt,,}
#Add extra \n if not empty due to "enter" inserting a newline
if [[ -n "$prompt" ]]; then
printf "\n"
fi
if [ "$prompt" == y ] || [ -z "$prompt" ]; then
# Clean journal entries older than 4 weeks
echo -e "\n${BLUE}Cleaning journal${NC}\n"
journalctl --vacuum-time=4weeks
echo -e "\n${GREEN}Finished cleaning journal\n"
fi
echo -e "${BLUE}"
read -n 1 -rp "Remove uninstalled packages files? [Y/n] " prompt
# Convert input to lowercase
prompt=${prompt,,}
#Add extra \n if not empty due to "enter" inserting a newline
if [[ -n "$prompt" ]]; then
printf "\n"
fi
if [ "$prompt" == y ] || [ -z "$prompt" ]; then
# Remove uninstalled pacman packages
echo -e "\n${BLUE}Removing uninstalled pacman packages${NC}\n"
paccache -ruk0
echo -e "\n${GREEN}Removed uninstalled pacman packages\n"
# Remove yay cache for foreign packages that are not installed anymore
echo -e "\n${BLUE}Removing uninstalled foreign packages${NC}\n"
rm -rf $removed
echo -e "${GREEN}Removed uninstalled foreign packages\n"
fi
echo -e "${BLUE}"
read -n 1 -rp "Clean pacman cache? [Y/n] " prompt
# Convert input to lowercase
prompt=${prompt,,}
#Add extra \n if not empty due to "enter" inserting a newline
if [[ -n "$prompt" ]]; then
printf "\n"
fi
if [ "$prompt" == y ] || [ -z "$prompt" ]; then
# Clean pacman cache
echo -e "\n${BLUE}Cleaning packages cache${NC}\n"
pkgcache="$(find $cachedir -mindepth 1 -maxdepth 1 -type d | xargs -r printf "-c %s\n")"
for pkgdir in "$cachedir"/*/; do
pkgname=$(basename "$pkgdir")
# Remove untracked files (e. g. source/build files) excepting package files and main source files for installed version if non-git package
if [[ ! "$pkgname" =~ ^.*-git$ ]]; then
pkgver="$(pacman -Q $pkgname | cut -d ' ' -f2 | cut -d '-' -f1 | cut -d ':' -f2)"
cd "$pkgdir"
rm -f $(git ls-files --others | grep -v -e '^.*\.pkg\.tar.*$' -e '^.*/$' -e "^.*$pkgver.*$" | xargs -r printf "$pkgdir/%s\n")
fi
rm -rf "$pkgdir"/src/
done
# Remove everything for uninstalled foreign packages and uninstalled native packages, keep two latest versions for installed packages
paccache -ruk0 $pkgcache
paccache -ruk0
paccache -rk2
echo -e "\n${GREEN}Finished cleaning packages cache\n"
fi
echo -e "${BLUE}"
read -n 1 -rp "Remove orphan packages? [Y/n] " prompt
# Convert input to lowercase
prompt=${prompt,,}
#Add extra \n if not empty due to "enter" inserting a newline
if [[ -n "$prompt" ]]; then
printf "\n"
fi
if [ "$prompt" == y ] || [ -z "$prompt" ]; then
# Remove orphan packages
echo -e "${BLUE}Removing orphan packages\n${RED}CHECK ALL PACKAGES AND EXPLICITLY INSTALL TO KEEP THEM${NC}\n"
pacman -Qdtq >"$DEST_FOLDER"/"$DEST_FILE"
chown "$SUDO_USER":"$SUDO_USER" "$DEST_FOLDER"/"$DEST_FILE"
echo -e "\n${GREEN}Saved orphan packages to file${NC}\n"
pacman -Rns $(pacman -Qdtq)
echo -e "\n${GREEN}Finished cleaning"
fi
printf "${NC}"