I have an image archive I keep up. Sometimes, the sites I pull them from reformat the file while keeping the extension the same, most often making PNG images into JPG's that are still named ".png". Is there a way to discover when this has happened and fix it automatically?

When on Windows, I used IrfanView for this, but that needs a Wine wrapper.


2 Answers 2


You can use file command:

$ file file.png
file.png: PNG image data, 734 x 73, 8-bit/color RGB, non-interlaced

$ mv file.png file.txt
$ file file.txt
file.txt: PNG image data, 734 x 73, 8-bit/color RGB, non-interlaced

The file does some tests on file to determine its type. Probably the most important test is comparing a magic number (string in a file header) with pre-defined list.

  • Exactly what I was looking for. Thank you!
    – Aescula
    Oct 28, 2014 at 3:03
  • 1
    When using file be aware that it can give differing output across the various Unixes. unix.stackexchange.com/questions/151008/…
    – slm
    Oct 28, 2014 at 3:17
  • On Ubuntu I get the error message: -bash: file: command not found. So we need to install the package first (I thought it comes with the default installation). You need to install "file" by sudo apt install file
    – Avatar
    Sep 27, 2022 at 6:10
  • Tipp: Use file --mime-type if you only want the MIME type itself without encoding information, e.g. application/pdf. Pass the option -b if you don't want to display the file name at the beginning of the line.
    – Avatar
    Sep 27, 2022 at 6:15

You can try imagemagicks identify command: http://www.imagemagick.org/script/identify.php


$ identify rose.jpg
rose.jpg JPEG 640x480 sRGB 87kb 0.050u 0:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .