Find Images by Size Using find, file, and awk

awkfilesfindimagesxargs

I've been trying to find png image files a certain height (over 500px). I know that file will return image dimensions. Example:

$ file TestImg1a.png

TestImg1a.png: PNG image data, 764 x 200, 4-bit colormap, non-interlaced   

But I need to use this to find all files in a directory with a height over 500px. I know how to print out all files regardless of height:

find . -name '*.png' | xargs file | awk '{print $7 " " $1}'

But how do I limit the $7 to those results greater than 500?

Best Answer

i know this is a bit overkill but, this will work every time (even if there are spaces in your filename) and regardless of how file displays the information.

find . -name '*.png' -exec file {} \; | sed 's/\(.*png\): .* \([0-9]* x [0-9]*\).*/\2 \1/' | awk 'int($1) > 500 {print}'

and it prints the dimensions of the picture and the file

explaination:

  1. find all files named *.png under . and for each do a file on it

  2. use sed to print only the filename and dimensions then re-order to print dimensions first

  3. use awk to test the first number (height of pic) making sure its greater than 500 and if it is print dimensions and file name, if not do nothing.

Related Question