Find images recursively, copy, rename, and resize?
How can I find images recursively and copy them into their own directories with new names? For example:
folder
- subfolder1
- a.jpg
- subfolder2
- b.jpg
- subfolder3
- c.jpg
So I can get:
folder
- subfolder1
- a.jpg
- a-copy.jpg
- subfolder2
- b.jpg
- b-copy.jpg
- subfolder3
- c.jpg
- c-copy.jpg
Ideally, I would like to resize the copied images to 50px width max (smaller than the original ones).
Any ideas?
First check with echo
included as follows (run from within the parent directory containing the folder
directory or alternatively provide /full/path/to/folder/*/*.jpg
below):
for f in folder/*/*.jpg
do
echo convert -geometry 50x "$f" "${f%.*}-copy${f//*./.}"
done
… and if satisfied with the naming output, remove echo
and run it again as follows:
for f in folder/*/*.jpg
do
convert -geometry 50x "$f" "${f%.*}-copy${f//*./.}"
done
… to do the actual resizing.
To include other file extensions (Case Sensitive), add them in a brace expansion like folder/*/*.{jpg,JPG,png,PNG}
etc.
Notice for image resizing, you’ll need convert
on your system … Also, notice the above convert -geometry 50x ...
line will enlarge images with width less than 50 and you might want to consider expanding it or using e.g. convert -resize 20% ...
instead.