Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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
add comment

2 Answers

up vote 4 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
add comment

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
add comment

Your Answer

 
discard

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.