Find images of a specific size and move them to trash from terminal
Im sorting images in a kinda large image library, and thumbnails from old iPhoto-library is still in there. And I'm in need of removing them to improve speed of sorting the images.
I have found this answer here for linux, but how about macOS? Is there a way of searching a folder, recursively, and determine if the image *.jpg, *.png, *.bmp, *.jpeg
have the W360 and H270?
Running Sierra 10.12.4
Solution 1:
Here is an example bash
script that can achieve the goal natively, no need to install anything.
#!/bin/bash
targetDir="$HOME/Pictures"
find "$targetDir" -iname '*.jpg' -o -iname '*.png' -o -iname '*.bmp' -o -iname '*.jpeg' 2>/dev/null | \
while read -r filename; do
hw="$(sips -g pixelHeight -g pixelWidth "$filename" 2>/dev/null)"
h="$(awk '/pixelHeight/{print $2}'<<<"$hw")"
w="$(awk '/pixelWidth/{print $2}'<<<"$hw")"
if [[ $h -eq 270 ]] && [[ $w -eq 360 ]]; then
echo rm "$filename"
fi
done
All you need to do is set the targetDir
variable to the starting point of where you want the find
command to look. The default is the Pictures folder within your Home folder.
Note: In the example script the echo
command needs to be removed from in front ofrm "$filename"
in order for the files to actually be deleted and is there so you can first test the output of the script as to what files will be deleted. You can of course choose to remove it without testing and just go for it, if you're confident that you want to delete any .jpg, .png, .bmp or .jpeg file with dimensions of 270 pixel height by 360 pixel width within the targetDir
.
Also note that depending on the total file count of the target file extensions, the running of this script can take some amount of time to complete. On my system it found ~1000 files to delete out of 27500 files in my Pictures folder and took at least 5 to 10 minutes to complete.
That said, I ran this script, as it's written, and the only files other then the test file I created with the target dimensions, all other files found where in one specific location within my iPhoto Library bundle in the Thumbnails folder. So, wouldn't it just be easier to go into the bundle and delete the contents in Finder? That is, if it's really just those Thumbnails you want to get rid of!
If you don't know how to use the script, do the following in Terminal:
touch delete270x360images
open delete270x360images
Copy and paste the script code from above into the opened delete270x360images
document, modify as needed/wanted and then save and close.
In Terminal, make it executable:
chmod u+x delete270x360images
Now to use it in Terminal, type the following and then press enter.
./delete270x360images
Wait for it to complete.