0

I have a folder of 500 pdf files. I need to combine 2 files into 1 file and create 250 pdf files

file1.pdf + file2.pdf >> combined1.pdf

file3.pdf + file4.pdf >> combined2.pdf

file499.pdf + file500.pdf >> combined250.pdf

can someone help in a simple sh script ?

Matiiss
  • 5,970
  • 2
  • 12
  • 29
novice
  • 11
  • 4
  • 1
    why is `python` tagged? you obviously ask for shell script (which is not exactly something you should do on SO anyways - ask someone to write code for you) – Matiiss Aug 02 '21 at 08:15

1 Answers1

0

It's not clear what you mean by "combine". I guess you can't just concatenate two PDF files. But that aside, something like this?

for this in file{[1-9],[1-9][0-9],[1-9][0-9][0-9]}.pdf; do
    case $this in
     *[13579].pdf)
        prev=$this; continue;;
     *) base=${this%.pdf}
        cat "$prev" "$this" >combined$((${base#file} / 2)).pdf;;
    esac
done

Obviously, replace the stupid cat with something which properly combines two PDF files into a valid single PDF file. Merge / convert multiple PDF files into one PDF has some commands but I'll leave it to you to choose which one to install.

The complex wildcard file{...}.pdf is because we want the files to be listed in numerical order, so we can't simply use file*.pdf.

The brace expansion file{[1-9],[1-9][0-9],etc}.pdf is a Bash feature. If you don't use Bash, spell it out, like file[1-9].pdf file[1-9][0-9].pdf fileetc.pdf

When we look at a file name whose number is odd, we just save that for the next iteration, and skip the rest of the loop. When we see an even-numbered file, we combine that with the one we remembered from the previous odd iteration.

(Obviously, if you have an odd number of files, the last one will be skipped.)

The parameter expansion ${this%.pdf} returns the value of $this with the .pdf suffix trimmed off. We then perform another parameter expansion ${base#file} to trim the static prefix file from the result, yielding just the number from the file name. We then divide that by two to produce the number for the combined file. The notation $(( ... )) produces an arithmetic context where simple integer arithmetic expressions are evaluated. (The shell otherwise doesn't evaluate numeric expressions at all.)

If your real names are named something1.pdf instead of file1.pdf etc, replace file in both the wildcard and in the substitution ${base#something}.

Demo: https://ideone.com/hyERFB

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • Thank you very much, this works perfect for txt and since i need to combine pdf, i am using cpdf. There is only one issue though, the result file is over written everytime and ultimately i got only pdf file instead of 250. Am I missing something here ? I just changed your cat line to cpdf "$prev" "$this" -o merged$((${base#file} / 2)).pdf;; My results is just one file merged0.pdf with the last 2 files in the folder combined – novice Aug 03 '21 at 04:23
  • I'm guessing your real file name is not `file123` in which case `${base#file}` does not return a number. – tripleee Aug 03 '21 at 04:54
  • Oh and also `file*.pdf` will not expand in numeric order if you have files with both single and double digits. I'll update the answer. – tripleee Aug 03 '21 at 05:03
  • This is awesome, not just for the solution also for the clarity and the speed with which you helped out here. I am sorry stackoverflow does nt allow me to upvote here. Thank you so much this help!! – novice Aug 03 '21 at 06:16