Permalink
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 44 lines (34 sloc) 1.01 KB
#!/usr/bin/env bash
#
# pbrisbin 2011
#
# Operates on current directory. Simply lists packages to be removed on
# stdout. Pipe to some other command (i.e. xargs rm) to take action as
# desired.
#
# Does not take into account the currently installed version. This tool
# simply saves the N most recent versions of each package found.
#
###
versions_to_keep=${1:-3}
parse() {
local package opt
while read -r package; do
case "$package" in
*gz) opt='-qxzf' ;;
*xz) opt='-qxJf' ;;
esac
bsdtar -O $opt "$package" .PKGINFO | awk -v package="$package" '/^pkgname/ { printf("%s %s\n", $3, package) }'
done
}
prune() {
local name package last_seen='' num_seen=0
while read -r name package; do
[[ -n "$last_seen" ]] && [[ "$last_seen" != "$name" ]] && num_seen=0
num_seen=$((num_seen+1))
# print full path
[[ $num_seen -gt $versions_to_keep ]] && readlink -f "$package"
last_seen="$name"
done
}
find ./ -maxdepth 1 -type f -name '*.pkg.tar.[gx]z' | LC_ALL='C' sort -r | parse | prune