View Full Version : [all variants] Shell script to resize jpg and subfolders

January 7th, 2011, 04:41 AM
I use the following to resize images in a folder

for file in `ls -l`
name=`echo $file | cut -f1 -d.`
convert -strip -quiet -geometry 1024x768 -quality 80 $file /path/to/pictures/{name}_email.jpg

Which works great. Every time I put a memory stick or camera card in my PC Shotwell imports them and saves them to my NAS in folders and subfolders which is also great. So the subfolder depth can vary. I would like to clone this structure but with all the images resized to 1024x768 with a cron job. Lazy eh.

Would it be possible to do this, automatically duplicate the file structure and resize the images into a completely different folder? In this case Dropbox. Whilst not recreating images already resized.

Best place to start?


January 7th, 2011, 10:20 AM
I would suggest a 2-step process ... (1) using rsync to mirror the directory structure, and (2) using find in a loop to do the converting ... you could combine both but that would complicate things a little.

something like the below. (try it out on a test sample first before unleashing it on the real thing!).


# set the source & dest directories -- (update to reflect your locations)

# sync the directory structure using rsync ... this uses rsync filter rules to
# ignore all files and only work on directories
cd "$src" || { echo "Failed to cd to $src, exiting..."; exit 1; }
rsync -a -f "+ */" -f "- *" . "$dest"

# find all jpg files, check if equivalent file exists in parallel directory,
# if not, create it
find . -iname '*.jpg' | while read file
if [[ ! -e "$other_file" ]]; then
echo "Converting $file to $other_file"
convert -strip -quiet -geometry 1024x768 -quality 80 "$file" "$other_fi
[[ $? -ne 0 ]] && { echo "Failed to create file $other_file, exiting"; e
xit 1; }
echo "Skipping $file as it already exists at $other_file"