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