Macos – Mac Terminal: Loop through subdirectories and optimise all images

image editingimagesmacosterminal

I have a folder containing many subfolders full of images. See attached image for example.

alt

Basically I want to loop through them all and downsize the images so none are wider than 1024 pixels. They're all jpegs.

I'm aware of the SIPS commands i.e.

sips -Z 1024 *.png

However, that only works if all images are in the current directory.

How do I set it up to traverse through all subdirectories?

Best Answer

You can run it with find: find images/path -type f -name '*.png' -exec sips -Z 1024 {} \;

Find will search for files (-type f), with png extension (-name '*.png') inside 'images/path' directory and exec the command in parameter, replacing "{}" with the filename, you need to end the command with "\;".

Related Question