title: Bash script pdftk merge PDFs
body: <p>I have a few thousand PDFs that I need merged based on filename.</p>
  
  <p>Named like: </p>
  
  <pre><code>Lastname, Firstname_12345.pdf
  </code></pre>
  
  <p>Instead of overwriting or appending, our software appends a number/datetime to the pdf if there are additional pages like: </p>
  
  <pre><code>Lastname, Firstname_12345_201305160953344627.pdf
  </code></pre>
  
  <p>For all the ones that don't have a second (or third) pdf the script doesn't need to touch. But, for all the ones that have multiples, they need to be merged into a new file <code>*_merged.pdf</code>? and the originals deleted.</p>
  
  <p>I gave this my best effort and this is what I have so far.</p>
  
  <pre><code>#! /bin/bash
  
  # list all pdfs to show shortest name first
  LIST=$(ls -r *.pdf)
  for x in "$LIST"
  
  # Remove .pdf extension. merge pdfs. delete originals.
  do
      y=${x%%.*}
      pdftk "$y"*.pdf cat output "$y"_merged.pdf
      find "$y"*.pdf -type f ! -iname "*_merged.pdf" -delete
  done
  </code></pre>
  
  <p>This script works to a certain extent. It will merge and delete the originals, but it doesn't have anything in it to skip ones that don't need anything appended to them, and when I run it in a folder with several test files it stops after one file. Can anyone point me in the right direction?</p>