0

I have a working script to generate thumbs to all pdf files in a folder( using Image Magic):

for f in *.pdf; do convert -thumbnail 250x200 "$f"[0]"_thumb_wd_${f%.pdf}.jpg"; done

How can i adjust this to check if the "_thumb_wd_+"f"+.jpg exists? So i can generate the thumbs only if it does not exists!

1 Answer 1

3

You can use if conditional construct to perform an action depending on some condition e.g. if something exists or not. In your case you need to put the action segment inside the if-then condition:

  if ! [[ -f "_thumb_wd_${f%.pdf}.jpg" ]]; then
    convert -thumbnail 250x200 "$f"[0]"_thumb_wd_${f%.pdf}.jpg"
  fi

[[ is a bash keyword, we are using it to check (-f) if the file _thumb_wd_${f%.pdf}.jpg exists and is a regular file. The ! negates the operation, as a result convert will only run if the file does not exist.

So, your final script:

#!/bin/bash
for f in *.pdf; do
  if ! [[ -f "_thumb_wd_${f%.pdf}.jpg" ]]; then
    convert -thumbnail 250x200 "$f"[0]"_thumb_wd_${f%.pdf}.jpg"
  fi
done
2
  • i was pretty close, but the "[[" got me here...can you tell me why the double "["?! May 5, 2015 at 20:01
  • @LcrossPortugal: yes, you were.. also note that [[ is an improved version of test ([) builtin of bash..you can check help test or the [[ section of man bash to get more idea on the conditions....
    – heemayl
    May 5, 2015 at 20:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .