Merging PDF files with similar names using PDFTK and a bash script

1.1k views Asked by At

I have a directory with a few hundred PDFs in it.

All of the PDFs filenames begin with a 5 digit number (and then have a bunch of other stuff at the end).

What I need to do is merge any PDFs together that start with the same 5 digit number.

Thoughts on how to do this via a shell script? Or other options? I'm using pdftk on Ubuntu.

1

There are 1 answers

3
Cyrus On BEST ANSWER

Try this:

find . -type f -iname "[0-9][0-9][0-9][0-9][0-9]*.pdf" -printf "%.5f\n" \
  | sort -u \
  | while read -r file; do 
         echo pdftk ${file}*.pdf cat output $file.pdf ;
    done

If output is okay, remove echo.