10

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.

jimmij
  • 46,064
  • 19
  • 123
  • 136
Aescula
  • 363
  • 3
  • 7
  • You can use free online tool [Falstaff](http://ec2-54-148-254-76.us-west-2.compute.amazonaws.com/falstaff/ "Falstaff") – Shaul Zevin May 01 '15 at 13:22

2 Answers2

17

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.

jimmij
  • 46,064
  • 19
  • 123
  • 136
  • Exactly what I was looking for. Thank you! – Aescula Oct 28 '14 at 03:03
  • 1
    When using `file` be aware that it can give differing output across the various Unixes. http://unix.stackexchange.com/questions/151008/linux-file-command-classifying-files/ – slm Oct 28 '14 at 03: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 '22 at 06: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 '22 at 06:15
5

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

Example:

$ identify rose.jpg
rose.jpg JPEG 640x480 sRGB 87kb 0.050u 0:01
Rui F Ribeiro
  • 55,929
  • 26
  • 146
  • 227
vfbsilva
  • 3,657
  • 3
  • 28
  • 42