Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Please, here is my little code for creating thumbnails of pictures saved in a folder named 'pictures', and saving them in another folder named 'thumbs'.

for i in *.jpg
convert -thumbnail 100 $i thumbs/$i

However, there two things the program doesn't do:

  1. It does not retain the name of the pictures in the thumbnail. For instance, I would want it to generate a thumbnail with the name pic.jpg for a picture named pic.jpg

  2. Also, when I run the program, i don't want it to generate the thumbnail for a picture it has already generated its thumbnail, unless that picture has been modified.

Any help will be very much appreciated.

share|improve this question

According to the ImageMagick documentation,

The ImageMagick command line consists of

  1. one or more required input filenames.
  2. zero, one, or more image settings.
  3. zero, one, or more image operators.
  4. zero, one, or more image sequence operators.
  5. zero, one, or more image stacks.
  6. zero or one output image filenames (required by convert, composite, montage, compare, import, conjure).

So you need to have the original image parameter before the -thumbnail 100 command.

To only run convert if the file exists, you need to add an if test.

Also, in the for loop you have for i in *.jpg but then in the convert command you have pictures/$i. Unless I misunderstand what you're doing, these should both be the same, as I've done in my example script. If you want the thumbs directory inside the pictures directory, remove the ../ part from the convert command and if test. You will need to run this from the pictures directory, or if you don't want to, you can add cd pictures to the start of the script.

So, the script should be something like this:

#! /bin/bash

for i in *.jpg; do
    if [ "$i" -nt "../thumbs/$i" ]; then
        convert "$i" -thumbnail 100 "../thumbs/$i";
share|improve this answer
This looks like it would work. – coteyr Nov 28 '12 at 3:09
Thank very much mspencer. Your solution is working perfectly – Duby Nov 28 '12 at 4:31
@Duby Great! If this is the correct solution, please mark my answer as your accepted answer. – iBelieve Nov 28 '12 at 15:34

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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