I have a lot of pdf files (3308) on which I must apply four steps:

1) I have to convert all to jpg

I found this little script on the web with ImageMagick: batch converting pdf to jpg

I want to do this, but I keep my files the same name as before: foo.pdf => foo.jpg Also I would like all images are saved in a folder in "scenes"

2) Then I have to resize to 612x792

3) Then I have to create thumbnails in 255x330

4) Finally I have to rename them. Indeed, I have a csv file in which is their names and new names

Here is an example of some lines.
Each line corresponds to the "actual name" file "comma" "the new name." There are 3308 rows, one per file


As you can see, the extensions are not displayed because the two files are in jpg format.

I am completely lost, I do not know whether to use 4 scripts or if it is possible in one script. I often work in PHP, but I wanted to do a bit of bash to change, but it's a bit hot for me.

Can you help me?

1 Answer 1


You can start like this:

for i in "$@"; do
  convert "$i" -resize 612x792 "$dst"
  convert "$i" -resize 255x330 "${i%.pdf}_thump.jpg"

And call it like

$ bash my_script.sh *.pdf

For renaming you can use another script. I don't understand your example .csv-file. Does is contain 3 lines for 3 files? Ok, this is the case.

You can rename the file with following command line:

$ awk -F, '{ system("echo mv \"" $1 "\" \"" $2 "\"")  }' myrename.csv

Awk executes the echo ... command for each line, $1 is the value of the first field of a line and $2 is the value of the 2nd field. The quoting \" is needed in case a filename contains spaces. -F, tells awk to use a comma as field separator.

If you have tested this command you can remove the echo to do real renaming of files. You can add -n to mv to avoid accidental overwrites of existing files.

  • Each line corresponds to the "actual name" file "comma" "the new name." There are 3308 rows, one per file
    – Raphaël
    Oct 7, 2011 at 9:24
  • @Raphaël, updated the answer Oct 7, 2011 at 9:58
  • The first part works. Approximately 15 minutes to convert, this is good. I test your second part. I do not think it to fit on a single order ^ ^!
    – Raphaël
    Oct 7, 2011 at 10:13
  • I delete the "echo" $ awk -F, '{ system("mv \"" $1 "\" \"" $2 "\"") }' myrename.csv ... for each line, I have an error : mv: cannot stat `S160_3237.jpg': No such file or directory ... but the file is rename. It works, but it's strange !
    – Raphaël
    Oct 7, 2011 at 10:35
  • @Raphaël, perhaps duplicated entries in myrename.csv? Oct 7, 2011 at 13:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.