Last active
February 19, 2021 21:27
-
-
Save digitaltrails/cfd6617be9f0e5cfbdaaf951d4526aef to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# OpenSUSE zypper - Purge off any /var/cache/zypp rpms that are not installed in this system | |
if [ $# -ne 0 -a "$1" != "--report-only" -a "$1" != "--purge" ] | |
then | |
>&2 echo "ERROR: invalid argument $1 - valid arguments are --report-only or --purge" | |
exit 1 | |
fi | |
>&2 echo "INFO: usage before $(du -h -s /var/cache/zypp)" | |
cd /var/cache/zypp && find . -type f -name '*.rpm' | gawk ' | |
BEGIN { | |
# Use rpm to initialise a hash table of all installed rpms | |
rpm_cmd = "rpm -q -a" | |
while ((rpm_cmd | getline) > 0) { | |
rpm_index[$0] = 1; | |
} | |
status = close(rpm_cmd); | |
if (status) { | |
print "ERROR: error running ", rpm_cmd, ERRNO > "/dev/stderr"; | |
exit 1; | |
} | |
if (length(rpm_index) < 1000) { | |
print "ERROR: too few rpms are installed, probably an error, cowardly refusing to continue ", length(rpm_index) ERRNO > "/dev/stderr"; | |
exit 1; | |
} | |
# Change field separator to / to ease extracting the filename only | |
FS = "/"; | |
} | |
{ | |
# For each file reported by find, check if it is not in the hash table | |
name_only = $NF; | |
sub(".rpm$", "", name_only); | |
if (!rpm_index[name_only]) { | |
# Output filenames to xargs to reduce the number of execs of rm to the mininum required. | |
print $0; | |
} | |
} | |
' | if [ $# -eq 0 -o "$1" == "--report-only" ] | |
then | |
>&2 echo "WARN: reporting only, no space will be recovered" | |
cat | |
elif [ "$1" == "--purge" ] | |
then | |
>&2 echo "WARN: commencing purge" | |
xargs --no-run-if-empty rm | |
>&2 echo "INFO: usage after $(du -h -s /var/cache/zypp)" | |
fi | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment