14

Ok, I got a directory (for instance, named '/photos') in which there are different directories (like '/photos/wedding', '/photos/birthday', '/photos/graduation', etc...) which have .jpg files in them. Unfortunately, some of jpeg files are broken. I need to find a way how to determine, which files are broken. I found out, that there is tool named imagemagic, which can help a lot. If you use it like this:

identify -format '%f' whatever.jpg

it prints the name of the file only if file is valid, if it is not it prints something like "identify: Not a JPEG file: starts with 0x69 0x75 `whatever.jpg' @ jpeg.c/EmitMessage/232.". So the correct solution should be find all files ending with ".jpg", apply to them "identify", and if the result is just the name of the file - don't do anything, and if the result is different from the name of the file - then save the name of the file somethere (like in a file "errors.txt").

Any ideas how I can probably do that?

1

4 Answers 4

17

The short-short version:

find . -iname "*.jpg" -exec jpeginfo -c {} \; | grep -E "WARNING|ERROR"

You might not need the same find options, but jpeginfo was the solution that worked for me:

find . -type f -iname "*.jpg" -o -iname "*.jpeg"| xargs jpeginfo -c | grep -E "WARNING|ERROR" | cut -d " " -f 1

as a script (as requested in this question)

#!/bin/sh
find . -type f \
\( -iname "*.jpg" \
 -o -iname "*.jpeg" \) \
-exec jpeginfo -c {} \; | \
grep -E "WARNING|ERROR" | \
cut -d " " -f 1

I was clued into jpeginfo for this by http://www.commandlinefu.com/commands/view/2352/find-corrupted-jpeg-image-files and this explained mixing find -o OR with -exec

2
  • find . -type f -iname "*.jpg" -exec identify -format '%f' {} \; 1>/dev/null # is faster then jpeginfo Feb 25, 2014 at 19:50
  • 6
    Unfortunately identify does not complain about some truncated jpg files I have, but jpeginfo -c does, so I recommend jpeginfo -c
    – iheggie
    Feb 22, 2016 at 8:40
15

One problem with identify -format is that it doesn't actually verify that the file is not corrupt, it just makes sure that it's really a jpeg.

To actually test it you need something to convert it. But the convert that comes with ImageMagick seems to silently ignore non-fatal errors in the jpeg (such as being truncated.)

One thing that works is this:

djpeg -fast -grayscale -onepass file.jpg > /dev/null

If it returns an error code, the file has a problem. If not, it's good.

There are other programs that could be used as well.

2
  • 2
    That was important to remark. Alexx's answer shows another tool to check a JPG file: jpeginfo -c file.
    – tokland
    Oct 10, 2016 at 19:22
  • 2
    Just in case, if you need to check all jpg files in folder do this -- for f in *.jpg; do djpeg -fast -grayscale -onepass $f > /dev/null; done
    – NG_
    Nov 3, 2017 at 15:40
8

You can put this into bash script file or run directly:

find -name "*.jpg" -type f |xargs --no-run-if-empty identify -format '%f' 1>ok.txt 2>errors.txt

In case identify is missing, here is how to install it in Ubuntu: sudo apt install imagemagick --no-install-recommends

6
  • 4
    Can also be written as find -name '*.jpg' -exec identify -format "%f" {} \; 1>ok.txt 2>errors.txt. Jun 4, 2010 at 13:22
  • 2
    Mark it as accepted, however, the final script is: find -name '*.jpg' -exec identify -format "%f\n" {} \; 2>errors.txt That might be exactly what I need, tested on test data and errors.txt give me all necessary info (ok.txt makes no good to me, so I deleted it from the script). Thanks all who participated!
    – Graf
    Jun 4, 2010 at 15:57
  • The command given in the answer gives me an error xargs: identify: No such file or directory
    – London guy
    May 1, 2018 at 15:12
  • @Graf thanks for noting that. London guy: Check first if identify is installed. May 3, 2018 at 11:18
  • @alexx-roche answer below is more relevant (detect partial jpegs)
    – filimonov
    Feb 3, 2021 at 18:07
2

This script will print out the names of the bad files:

#!/bin/bash

find /photos -name '*.jpg' | while read FILE; do
    if [[ $(identify -format '%f' "$FILE" 2>/dev/null) != $FILE ]]; then
        echo "$FILE"
    fi
done

You could run it as is or as ./badjpegs > errors.txt to save the output to a file.

To break it down, the find command finds *.jpg files in /photos or any of its subdirectories. These file names are piped to a while loop, which reads them in one at a time into the variable $FILE. Inside the loop, we grab the output of identify using the $(...) operator and check if it matches the file name. If not, the file is bad and we print the file name.

It may be possible to simplify this. Most UNIX commands indicate success or failure in their exit code. If the identify command does this, then you could simplify the script to:

#!/bin/bash

find /photos -name '*.jpg' | while read FILE; do
    if ! identify "$FILE" &> /dev/null; then
        echo "$FILE"
    fi  
done

Here the condition is simplified to if ! identify; then which means, "did identify fail?"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.