Skip to content

Instantly share code, notes, and snippets.

@aakbar5
Created July 16, 2019 10:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aakbar5/cf8b10987f252a5793fef70be271d041 to your computer and use it in GitHub Desktop.
Save aakbar5/cf8b10987f252a5793fef70be271d041 to your computer and use it in GitHub Desktop.
Merge pdf files
#!/bin/bash
# This script merge all pds found in a folder
# Where $1 is folder name
FOLDER="$1"
FILE_COUNT="$(ls $FOLDER/*.pdf | wc -l)"
NEW_NAME="merged-pdfs-$FILE_COUNT-$(cat /dev/urandom | tr -dc '0-9a-zA-Z' | fold -w 16 | head -n 1).pdf"
CMD="gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=$NEW_NAME"
# Get all files and append to command
for file in "$FOLDER"/*.pdf; do CMD="$CMD $file"; done
echo "Folder: $1"
echo "File Count: $FILE_COUNT"
echo "Pdf: $NEW_NAME"
# echo "Cmd: $CMD"
echo ""
echo "Merge in progress..."
ret="$($CMD)"
echo "*** $NEW_NAME is ready."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment