You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536
  1. #!/usr/bin/env bash
  2. #
  3. # pbrisbin 2013 - list packages for which more than N versions exist in
  4. # the current directory.
  5. #
  6. ###
  7. versions_to_keep=${1:-3}
  8. parse() {
  9. local package opt
  10. while read -r package; do
  11. case "$package" in
  12. *gz) opt='-qxzf' ;;
  13. *xz) opt='-qxJf' ;;
  14. esac
  15. bsdtar -O $opt "$package" .PKGINFO |\
  16. awk -v pkg="$package" '/^pkgname/ { printf("%s %s\n", $3, pkg) }'
  17. done
  18. }
  19. prune() {
  20. local name package last_seen='' num_seen=0
  21. while read -r name package; do
  22. [[ "${last_seen:-$name}" != "$name" ]] && num_seen=0
  23. (( ++num_seen > versions_to_keep )) && readlink -f "$package"
  24. last_seen="$name"
  25. done
  26. }
  27. find ./ -maxdepth 1 -type f -name '*.pkg.tar.[gx]z' |\
  28. LC_ALL='C' sort -rV | parse | prune