Reputation: 1261
I have a bunch of sub-folders with images in them. I am trying to recursively convert them into pdf files using the directory names as the pdf name. With the help of some Google searches I tried using this script I wrote:
#!/bin/bash
for D in `find . -type d` do
convert *.jpg ${PWD##*/}.pdf
end
It did not work. How can I get this to work?
Each folder has several .JPG
s in it with a number 01-10 etc. Using convert *.jpg name.pdf
converts all the images into one pdf file. I want to make a script to do this and for the PDF files to have the directory name.
I would also like the script to then grab the converted pdf and move it up a level into the parent directory.
Upvotes: 1
Views: 3072
Reputation: 11
Here's a version of savanto's answer that uses img2pdf, which is faster, lossless, and makes smaller file sizes than ImageMagick.
`find . -type d | while read d; do img2pdf "${d}"/*.jp2 --output ./"${d##*/}.pdf"; done`
Upvotes: 1
Reputation: 85
Thanks to @savanto for the answer. I modified it a bit for my needs.
Read through the files in a directory's subdirectories. Convert one file type to a different file types. Name the file and save it to a directory (using OSX 10.10, terminal bash-3.2).
cd /parent_directory
find . -type d | while read d; do mogrify -format png *.eps; done
find . -name '*.eps' -execdir mogrify -format png {} \;
Upvotes: 1
Reputation: 13549
Easy:
find path/to/images -iname "*.jpg" | xargs -I %this convert %this %this.pdf
Very simple and clean!
Upvotes: 0
Reputation: 4550
A while
loop is more appropriate here. Try this:
find . -type d | while read d; do convert "${d}"/*.jpg ./"${d##*/}.pdf"; done
find
will return all directories in current directory.while read d
will read each directory path into variable $d
.convert ${d}/*.jpg
performs the conversion on all .jpg images in directory $d
../${d##*/}.pdf
replaces the whole directory path with just the directory name, appends .pdf
, and ensures that PDF file is created in parent directory.Upvotes: 5