Drew S Drew S - 8 months ago 45
Linux Question

convert images to pdfs in subdirectories

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:


for D in `find . -type d` do
convert *.jpg ${PWD##*/}.pdf

It did not work. How can I get this to work?

Each folder has several
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.


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.