0

I have the original images in a directory structure that looks like this:

./Alabama/1.jpg
./Alabama/2.jpg
./Alabama/3.jpg
./Alaska/1.jpg
...the rest of the states...

I wanted to convert all of the original images into thumbnails so I can display them on a website. After a bit of digging / experimenting, I came up with the following Linux command:

find . -type f -iname '*.jpg' | sed -e 's/\.jpg$//' | xargs -I Y convert Y.jpg -thumbnail x100\> Y-small.jpg

It recursively finds all the jpg images in my subdirectories, removes the file type (.jpg) from them so I can rename them later, then makes them into a thumbnail and renames them with '-small' appended before the file type.

It worked for my purposes, but its a tad complicated and it isn't very robust. For example, I'm not sure how I would insert 'small-' at the beginning of the file's name (so ./Alabama/small-1.jpg).

Questions:

  1. Is there a better, more robust way of creating thumbnails from images that are located in multiple subdirectories?
  2. Can I make the existing command more robust (for example, but using sed to rename the outputted thumbnail before it is saved- basically modify the Y-small.jpg part).

1 Answer 1

0

If it is always just one level of directories below your position, you could try looping this way:

for dir in *; do
  [ ! -d "$dir" ] && continue; # skip non-directory entries, i.e. additional files
  cd "$dir"
  for file in *.jpg; do
    convert -thumbnail x100 "$file" "small-${file}" # creates e.g. small-1.jpg
  done
done

You must log in to answer this question.

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