Karim Bn Abdlaziz
Karim Bn Abdlaziz

Reputation: 57

merge same name multiple part pdf files by order

how merge same name's pdf files

  1. poetry_2.pdf

  2. poetry_3.pdf

  3. poetry_4.pdf

  4. metaphysics_2.pdf

  5. metaphysics_3.pdf

i look for

failed this loop to check pdf files and merge with pfunite

for file1 in *_02.pdf ; do
    # get your second_ files
    file2=${file1/_02.pdf/_03.pdf}
    # merge them together
    pdfunite $file1 $file2  $file1.pdf
    
done

Upvotes: 0

Views: 187

Answers (1)

Socowi
Socowi

Reputation: 27215

First, you need a list of prefixes (e.g. poetry, metaphysics). Then, iterate over that list and unite prefix_*.pdf into prefix.pdf.

Here we generate the list of prefixes by searching for files ending with _NUMBER.pdf and removing that last part. This assumes that filenames do not contain linebreaks.

printf %s\\n *_*.pdf | sed -En 's/_[0-9]+\.pdf$//p' | sort -u |
while IFS= read -r prefix; do
  pdfunite "$prefix"_*.pdf "$prefix.pdf"
done 

Upvotes: 3

Related Questions