autorename-pdf 460 B

123456789101112131415161718192021
  1. #!/usr/bin/env bash
  2. set -euo pipefail
  3. IFS=$'\n\t'
  4. if [ $# -lt 1 ] ; then
  5. echo "Usage: $(basename $0) PDF-NAME+"
  6. exit 1
  7. fi
  8. while (( "$#" )) ; do
  9. OLD_NAME="${1}"
  10. FILE_SLUG=$(pdfinfo $OLD_NAME | grep Title | cut -d: -f 2 | sed 's/ *$//g' | sed 's/^ *//g' | tr 'A-Z' 'a-z' | tr ' ' '-')
  11. if [ ! -z $FILE_SLUG ] ; then
  12. mv "$OLD_NAME" "${FILE_SLUG}.pdf"
  13. else
  14. echo "Cannot detect title in ${OLD_NAME}"
  15. fi
  16. shift
  17. done