Wednesday, May 8, 2024
13
rated 0 times [  13] [ 0]  / answers: 1 / hits: 22451  / 2 Years ago, sun, december 19, 2021, 10:27:00

I know that the command convert *.jpg myPdf1.pdf can convert multiple JPEG files into a single PDF.



But I would like to convert multiple JPEGs into multiple PDFs, for example:



myJPG1.jpg → myPDF1.pdf
myJPG2.jpg → myPDF2.pdf
myJPG3.jpg → myPDF3.pdf


Is there any decent way to manage something like that?


More From » command-line

 Answers
0

My first instinct for batch processing files is almost always find. It's excellent if you need to build in any sort of filtering (which you don't here) but it's still a favourite. This will also recurse into subdirectories unless you tell it (with -maxdepth 1 or other):



find -name '*.jpg' -exec convert "{}" "{}.pdf" ;
rename 's/.jpg.pdf$/.pdf/' *.jpg.pdf


The find/convert statement will output a load of .jpg.pdf files. The second cleans this up.






Perhaps a slightly more elegant approach in such a simple case:



for file in *.jpg ; do convert "$file" "${file/%jpg/pdf}"; done


This doesn't recurse and you don't have to mess around cleaning up the filenames.






And I almost forgot, ImageMagick has a numerical output which might fit your use-case perfectly. The following will just stick a three-digit identifier (000, 001, 002, etc) on the end of the "myPDF":



convert *.jpg myPDF%03d.pdf


Obviously if you're dealing with more than a thousand entries, increase the number. If you don't want it zero-padded, remove the leading zero.


[#25050] Tuesday, December 21, 2021, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
hirtieve

Total Points: 207
Total Questions: 104
Total Answers: 114

Location: Cook Islands
Member since Thu, May 21, 2020
4 Years ago
hirtieve questions
;