-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautoSystemCleaning
More file actions
executable file
·43 lines (27 loc) · 1.45 KB
/
autoSystemCleaning
File metadata and controls
executable file
·43 lines (27 loc) · 1.45 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
#!/usr/bin/env bash
# Equivalent of the maintenanceCleaning script but made for use as a systemd service
# and timer to run periodically
# Package cache cleaning implementation from @albertored11
# Assuming yay is run by user with UID 1000
admin="$(id -nu 1000)"
# Clean journal entries older than 4 weeks
usr/bin/journalctl --vacuum-time=4weeks
cachedir="/home/$admin/.cache/yay"
removed="$(comm -23 <(basename -a $(find $cachedir -mindepth 1 -maxdepth 1 -type d) | sort) <(pacman -Qqm) | xargs -r printf "$cachedir/%s\n")"
# Remove yay cache for foreign packages that are not installed anymore
rm -rf $removed
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
/usr/bin/paccache -qruk0 $pkgcache
/usr/bin/paccache -qruk0
/usr/bin/paccache -qrk2 -c /var/cache/pacman/pkg $pkgcache