Super User is a question and answer site for computer enthusiasts and power users. 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

I have very basic shell scripting knowledge.

I have photos under original folder on many different folder like this

folder
  + folder1
    + original
  + folder2
    + original
  + folder3
    + original
  + folder4
    + original

Using mogrify I'm trying to create thumbs under a thumb folder following a structure to this.

folder
  + folder1
    + original
    + thumb
  + folder2
    + original
    + thumb
  + folder3
    + original
    + thumb
  + folder4
    + original
    + thumb

I'm a little lost in how to write the shell script that may iterate through it. I'm ok giving mogrify its settings but I don't complete understand how to tell the script to go iterate each folder to run the mogrify command.

share|improve this question
    
How many files are we talking about--total? – ephsmith Jul 6 '12 at 6:12
up vote 3 down vote accepted

Rather than use a for loop where you will have issues if there are spaces in any of the file paths, you can use find to recursively look under a directory and take an action on all files whose name matches a pattern. If all files will need to be affected, just leave out the -name option or customize it to fit.

find /path/to/directory -type f -name "*.png" -exec mogrify {} +

To make better use of mogrify's options, you can use xargs.

find /path/to/directory -type f -name "*.png" | xargs mogrify -path /path/to/thumbnails
share|improve this answer
    
Good point. I like it. Is there a modification to this that would generate the resulting directory structure that the OP desires? mogrify will be run in the current directory. – ephsmith Jul 6 '12 at 6:11
    
Yes, mogrify takes an output parameter as shown in ephsmith's solution: mogrify -path ./thumbnails/ $f – lonstar Jul 6 '12 at 10:29

I'm pretty sure you'll see a great deal of variation in the responses you'll receive. There are more than a few ways to do it. The following approach simply uses ls to list directories in the current directory. It then changes to that directory to get a list of png files (modify to suit). For each of the png files, mogrify is called.

Basically, you'll need to

  1. Copy this into a text editor.
  2. Modify the line containing mogrify command to suit.
  3. For simplicity, save it as a plain-text file to the directory in which you want to run it.
  4. Set the execute bit for the file with chmod +x /path/to/scriptname
  5. Run it from the directory with ./scriptname

Script below:

DIRS="$(find . -mindepth 1 -maxdepth 2 -type d)"

for d in $DIRS
do
    cd $d
    FILES="$(find . -name '*.png')"
    for f in $FILES
    do
    mogrify -path ./thumbnails/ $f
    done
    cd ..
done
share|improve this answer

You must log in to answer this question.

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