Mac Terminal: Loop through subdirectories and optimise all images
5,905
Solution 1
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 "\;".
Solution 2
I managed to change a small bash script which worked for me
#!/bin/bash
find "foldername" -type f | \
while read file ; do
echo "processing ${file}"
sips -Z 2000 ${file}
done
Related videos on Youtube
Author by
Quadrant6
Updated on September 18, 2022Comments
-
Quadrant6 almost 2 years
I have a folder containing many subfolders full of images. See attached image for example.
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?
-
Quadrant6 almost 10 yearsThanks. I had some issue with that not actually resizing all but did the same with imagemagick:
find /images/path -type f -iname "*.jpg" -execdir convert {} -resize 1024x1024\> -quality 70 {} \;
-
Quadrant6 almost 10 yearsThanks, I'm trying the sips command again, it runs through as if it's doing something but doesn't seem to actually save the new file..?
find images/path -type f -name "*.jpg" -exec sips -Z 1024 -s format jpeg -s formatOptions 80 {} \;
-
denisvm almost 10 years@Quadrant6 check if it's not saving the file in current directory, also test the sips command alone in the same path but specifying the full pathname for a test image file.
-
Guilherme over 4 yearsperfect to remove files: find "FOLDER_NAME" -type f -name '*_16.png' -exec rm {} \; Thanks!!!!!
-
denisvm about 4 years@Guilherme You can use
-delete
instead of-exec rm {} \;
if you want just to remove files:find "FOLDER_NAME" -type f -name '*_16.png' -delete