Skip to content

Instantly share code, notes, and snippets.

@co60ca
Created April 24, 2019 19:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save co60ca/1dec52ff4122aeb7f5227d7c62513812 to your computer and use it in GitHub Desktop.
Save co60ca/1dec52ff4122aeb7f5227d7c62513812 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Copyright Maeve Kennedy 2019
# Under MIT License
set -eu
if ! [ "${1:-""}" ] ; then
echo "Usage: $0 filename..." >&2
echo " converts filename... to <pdf title>.pdf" >&2
exit 1
fi
if ! which pdftotext > /dev/null ;
echo "pdftotext is required, please install or add to path" >&2
exit 1
fi
mkdir -p backups
for file in "$@"; do
# File exist Guard
if ! [ -f "$file" ] ; then echo "$file" not found >&2 ; continue ; fi
txt=`pdftotext -htmlmeta "${file}" /dev/stdout | head -n 3`
[[ "$txt" =~ \<title\>(.*)\</title\> ]]
newname="${BASH_REMATCH[1]:-""}"
# Guard
if ! [ "${newname}" ] ; then echo "$file" has no pdf title >&2 ; continue ; fi
newname="${newname}.pdf"
# Replace non alpha words, html encoding
if which recode > /dev/null & which sed > /dev/null ; then
newname=`echo "${newname}" | recode html`
newname=`echo "${newname}" | sed 's/<.*>//g'`
fi
# Replace / 's
newname="${newname//\//-}"
# Replace spaces with dashes
newname="${newname//[[:space:]]/-}"
echo "$file -> $newname"
cp "${file}" backups
mv "${file}" "$newname"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment