Skip to content

Instantly share code, notes, and snippets.

@ShannonScott
Last active October 10, 2019 16:37
Show Gist options
  • Save ShannonScott/8238e607803ab169a280a0fe00a1c28f to your computer and use it in GitHub Desktop.
Save ShannonScott/8238e607803ab169a280a0fe00a1c28f to your computer and use it in GitHub Desktop.
[Rename PDFs by Title] Rename a directory of PDF files by their title as reported by `pdfinfo`. #tags: bash, pdf

Rename PDFs by Title.md

Rename a directory of PDF files by their title as reported by pdfinfo.

Bash script

#!/bin/bash
# Rename all PDFs in current dir to their title as reported by `pdfinfo`

mkdir -p "renamed"
mkdir -p "skipped"

for file in *.pdf
do
    # Extract title, strip leading and trailing whitespace
    PDFTITLE=$(pdfinfo ${file} |  awk -F':' '$1=="Title" {print $2}' | xargs)

    if [ -z "$PDFTITLE" ]
    then
        echo "Skipping $file (title missing)"
        cp $file skipped/
    else
        echo "${PDFTITLE}.pdf"
        cp $file "renamed/${PDFTITLE}.pdf"
    fi
done

Alternate whitespace stripping

pdfinfo ${file} |  awk -F':' '$1=="Title" {print $2}' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//'

Links

#!/bin/bash
# Rename all PDFs in current dir to their title as reported by `pdfinfo`
mkdir -p "renamed"
mkdir -p "skipped"
for file in *.pdf
do
# Extract title, strip leading and trailing whitespace
PDFTITLE=$(pdfinfo ${file} | awk -F':' '$1=="Title" {print $2}' | xargs)
if [ -z "$PDFTITLE" ]
then
echo "Skipping $file (title missing)"
cp $file skipped/
else
echo "${PDFTITLE}.pdf"
cp $file "renamed/${PDFTITLE}.pdf"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment