0

I have 86 PDFs that I don't want to share on the internet and merge with online tools. I am using the PopOS Linux distro and I would like to merge it using the terminal.

PDF names are like 1.SubjectA, 2. SubjectB (start with Number. , so they are well ordered) Here is what I found but none of them are merging in order:

qpdf --empty --pages *.pdf -- out.pdf

Example file names:

1. Why to learn System and Network.pdf
2. Network, Hardwares, LAN-WAN.pdf
3. Protocols-Ports, OSI-TCP IP.pdf
4. ARP, ICMP, RFC, IANA.pdf
...

Pattern is Number + .(dot) + space + name

  • Create a bash script with this content (and give execution perms): qpdf --empty --pages "$@" -- out.pdf and in your terninal type: printf -- "%s\0" ./*.pdf | sort -zV | xargs -0 ./script – Edgar Magallon Dec 14 '22 at 08:48

2 Answers2

0

I would try to tune a sort command to match the required order:

qdf --empty --pages `ls *.pdf |sort -n` -- out.pdf

This approach will not work if filenames include spaces. In this case, I propose:

find . -name "*.pdf" -type f -print0  |sort -V -z |xargs -0 qpdf --empty --pages {} -- out.pdf

If this does not fill your needs, please explain the problems you find.

jonasmike
  • 169
0

With all the files in the one directory one can get rid of the spaces and commas in the filenames, then sort them and merge them, thus:

 find . -name "*" -type f | rename 's/ /_/g'
 find . -name "*" -type f | rename 's/,/_/g'
 ls | sort -n | pdfunite *.pdf merged.pdf
currawong
  • 178