سؤال

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 .JPGs 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.

نصائح أخرى

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 {} \;

Easy:

find path/to/images -iname "*.jpg" | xargs -I %this convert %this %this.pdf

Very simple and clean!

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top