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