Finding PNG Images Larger Than x Pixels Through the Linux Shell

By Lowell Heddings on December 20th, 2007

When you are trying to work on changing the design of your website, you have to be concerned with the width of the pictures in your article content. I’ve got notoriously large screenshots on most of the articles I’ve written, so if I want to increase the sidebar it’s critical to figure out which pictures are going to be too wide to fit in the new design.

Since I’m a programmer, it would be easy for me to write a small application to do this, but it made me start thinking… why can’t I do this on the Linux command line?

The first thing I figured out was that PNG images display the size data when you run the “file” command on them:

$ file image3.png
image3.png: PNG image data, 613 x 657, 8-bit/color RGBA, non-interlaced

Very useful since 99% of the picture on this site are in PNG format. So now to throw it in a loop for all the files in my upload directory:

$ for f in *.png;do file $f;done

image.png: PNG image data, 631 x 185, 8-bit/color RGBA, non-interlaced
image1.png: PNG image data, 631 x 96, 8-bit/color RGBA, non-interlaced
image10.png: PNG image data, 375 x 395, 8-bit/color RGBA, non-interlaced
image11.png: PNG image data, 484 x 241, 8-bit/color RGBA, non-interlaced

This is more useful, but I’d have to pull the data into Excel or a similar application in order to sort the data, so I decided to use the linux “cut” command to pull out just the width column.

You’ll notice the -f5 parameter tells cut to take the fifth column, and the -d\ with a space after it tells cut to use a space as the delimiter. The slash \ character is an escape character to tell the shell to use the space as a character, and not as whitespace.

$ for f in *.png;do file $f|cut -f5 -d\ ;done


Not entirely useful output, is it? Let’s push that through a bash if statement, and then only show the output of the file command when the width is larger than 600 pixels.

Notice the ` (backtick) marks around the “file $f | cut…” section, which indicate that the commands inside the ` will be processed as a single output and fed into the if statement, where we use a -gt (greater than). Also note that you need spaces around either side of the brackets [ ]

for f in *.png;do if [ `file $f | cut -f5 -d\ ` -gt 600 ] ; then file $f;fi;done

image.png: PNG image data, 631 x 185, 8-bit/color RGBA, non-interlaced
image1.png: PNG image data, 631 x 96, 8-bit/color RGBA, non-interlaced
image17.png: PNG image data, 638 x 340, 8-bit/color RGBA, non-interlaced
image18.png: PNG image data, 608 x 448, 8-bit/color RGBA, non-interlaced

Now we have a list of all the files larger than 600 pixels wide. You could adjust the “file $f” at the end to just echo out the filenames if you needed to copy or move them somewhere else:

for f in *.png;do if [ `file $f | cut -f5 -d\ ` -gt 600 ] ; then echo $f;fi;done


The Linux shell is incredibly powerful! This solution isn’t really practical for everybody, but it’s good to know how to work with the shell so you can accomplish this type of task when you need to.

Lowell Heddings, better known online as the How-To Geek, spends all his free time bringing you fresh geekery on a daily basis. You can follow him on if you'd like.

  • Published 12/20/07
More Articles You Might Like

Enter Your Email Here to Get Access for Free:

Go check your email!