Using command line to find image of a certain size

Just a quick post of a solution to a problem I was faced with today…

So I have a directory with about 8,000 images in it and I needed to find an image that was 280 pixels in width.  How to find the images using the command line?

Well here is what I came up with:

for f in *.jpg;do if [ `identify $f | cut -f3 -d\ | cut -f1 -dx` -eq 280 ] ; then echo $f;fi;done

Works nice..  There are other versions of this out there, but they work on PNG’s not JPEG’s because the “file” commend does not return the size of a JPEG.

# file somefile.png
somefile.png: PNG image, 755 x 70, 8-bit/color RGB, non-interlaced
# file somefile.jpg
somefile.jpg: JPEG image data, JFIF standard 1.01

Bummer; but “identify” does return size data, cool..

# identify somefile.jpg
somefile.jpg JPEG 80x100 80x100+0+0 8-bit DirectClass 2.64KiB 0.000u 0:00.000

Cut the output and check for the matching size.. easy!