0

I would like to create thumbnails of images I have organised into a set of nested subdirectories into a mirror of the file structure so that a command of the type:

./imageresize.sh Large Small 10

...would convert any .jpg or .JPG files, in the directories nested under "./Large":

./Large/Holidays/001.jpg
./Large/Holidays/002.jpg
./Large/Holidays/003.jpg
./Large/Pets/Dog/001.jpg
./Large/Pets/Dog/002.jpg
./Large/Pets/Cat/001.jpg

into thumbnails of 10% to a mirror destination with a different top directory ("Small" instead of "Large" in this e.g.):

./Small/Holidays/001.jpg
./Small/Holidays/002.jpg
./Small/Holidays/003.jpg
./Small/Pets/Dog/001.jpg
./Small/Pets/Dog/002.jpg
./Small/Pets/Cat/001.jpg

This is what I have so far, but I can't seem to get it working. The $newfile variable seems invalid but I don't know why, and when testing, it outputs the result of the 'convert' command to the screen. Any help/suggestions greatly appreciated.

#!/bin/bash

#manage whitespace and escape characters
OIFS="$IFS"
IFS=$'\n'

#create file list
filelist=$(find ./$1/ -name "*.jpg" -o -name "*.JPG")

for file in $filelist
do

#create destination path for 'convert' command
newfile= ${file/$1/$2}

convert "$file" -define jpeg:extent=10kb -scale $3% "$newfile"
done
1
  • Haven't looked at anything else, but at least there is a space after newfile=.
    – 4ae1e1
    Nov 17, 2015 at 19:07

2 Answers 2

0

Don't know if it's just a copy/paste error or if it's actually in your script, but this line:

newfile= ${file/$1/$2}

would be an invalid assignment in Bash as spaces around = isn't allowed when assigning.

Try this instead:

newfile=${file/$1/$2}

As a side-note. find has a case-insensitive search too, -iname, so you could do:

filelist=$(find ./$1/ -iname "*.jpg")

It also has -exec for executing commands on the result set. It's explained very well here: https://unix.stackexchange.com/questions/12902/how-to-run-find-exec So you may be able to do it in one single find. (Note: That's not necessarily better, in most cases it's just a matter of preference, I just mention it as a possibility.)

1
  • 1
    Thankyou for the reply! ...and the -iname option is perfect. I've corrected the formatting as you suggested and it fixed one issue that I didn't actually realise was an issue, and I've just found the other problem which is that I need the directories to be created before converting! So I've added the line: rsync -a --include '/' --exclude '' ./$1/ ./$2/ - not very elegant, but it works - I'll post the working script as a separate answer below. Thanks again!
    – R Ted
    Nov 17, 2015 at 19:25
0

So - revised working script with corrections suggested by madsen and 4ae1e1 (thanks both), and with rsync commands to create directory structure first, (then clean extraneous files from the destination) :). I've added an extra parameter and parameter checker so that now you can specify source, destination, approx destination file size in kb and percentage of original. Hope it helps someone else. :)

#!/bin/bash

#manage whitespace and escape characters
OIFS="$IFS"
IFS=$'\n'

#check parameters
if [ $# -lt 4 ] || [ "$1" = "--help" ]
then # if no parameters or '--help' as $1 - show help
    echo "______________________________________________________________"
    echo ""
    echo "Useage: thumbnailmirror [Source] [Destination] [Filesize] [Percentage]"
    echo "Source - e.g. 'Fullsize' (directory must exist)" 
    echo "Destination - e.g. 'Thumnail' (directory must exist)"
    echo "Filesize - approx filesize in kb  e.g. '10'"
    echo "Percentage - % of reduction (1-100)"
    echo "e.g. thumbnailmirror Fullsize Thumnail 18 7"
    echo "______________________________________________________________"


else # parameters exist
    #syncronise directory structure (directories only)
    rsync -a --include '*/' --exclude  '*'  ./$1/ ./$2/
    # delete any extraneous files and directories at destination
    rsync -a --delete --existing --ignore-existing ./$1/ ./$2/
    #create file list ( -iname means not case sensitive)
    filelist=$(find ./$1/ -iname "*.jpg")

    for file in $filelist
      do
      #define destination filename for 'convert' command
      newfile=${file/$1/$2}

      if [ ! -f "$newfile" ] #if file doesn't exists create it
      then
          convert "$file" -define jpeg:extent=$3kb -quality 100 -scale $4% "$newfile"
          echo "$file resized"
      else #skip it
          echo "Skipping $file - exists already"
      fi
      done
fi
4
  • A script that starts with OIFS="$IFS" and IFS=$'\n' is very likely a broken script! Dec 10, 2015 at 12:56
  • It seems to work for me as it is, I copied and pasted that bit in an attempt to handle whitespace/escape characters from another script, but if you could point me in the right direction to correct the mistake I'd be grateful! :)
    – R Ted
    Dec 11, 2015 at 20:21
  • Well, things will badly break when you have glob characters (e.g., *, ?, [ or ]) or newlines in your filenames. A good pointer, to start with, is this one: mywiki.wooledge.org/BashFAQ/001 Dec 11, 2015 at 20:28
  • Thank you! I'll check it out. :)
    – R Ted
    Dec 11, 2015 at 20:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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