Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a somewhat-kludgey script in which I want to copy valid, complete JPEG files to a photo album directory. I thought I could use ImageMagick's identify command to do this. The web documentation for that command says

The identify program describes the format and characteristics of one or more image files. It also reports if an image is incomplete or corrupt.

... which sounds great. And if I run identify jpeg:testfile.jpg, I get an exit code of 0 if the test file is a JPEG, and 1 if it isn't. Good start — but the command also returns 0 if the file appears to be a JPEG but is incomplete. I looked through the extensive list of options, but nothing seems relevant. Can I use this command to do what I want? How?

share|improve this question
Have you tried with -regard-warnings? "It also causes IM to return a proper exit status according to such error conditions." hoernle1.de/m/imagick6/basics/index.html#regard-warnings – jasonwryan Sep 6 '11 at 3:19
@jasonwryan: Wow, that's significantly more descriptive than the text from imagemagick.org, which just says (in entirety) "Pay attention to warning messages." The version on my CentOS 5-based server is missing that option, but I tried it on a Fedora box, and it seems to return success even on severely truncated images. – mattdm Sep 6 '11 at 3:35
I can get it to exit with the correct code if the image is corrupt (by redirecting shredded bytes to a .jpg), but I don't have a truncated file to test on. Sorry: that's all I've got. – jasonwryan Sep 6 '11 at 4:04
It's easy to make a truncated copy with dd.... – mattdm Sep 6 '11 at 4:08
OK: dd'ed a .jpg and it accurately reports it corrupted if I overwrite the start of the file (header etc), but reports it as good if I truncate it. Bug? – jasonwryan Sep 6 '11 at 4:22
up vote 7 down vote accepted

If an alternate program is an option, I think I used jpeginfo -c the last time I needed to check the validity of a bunch of JPEG files.

share|improve this answer
jpeginfo's output shows tags like: [ERROR] [WARNING] or [OK], Here is a sample message: ... 37768 Premature end of JPEG file [WARNING]... In this case, 37768 is the file length in bytes.... Howerver, identify did not report a warning for this particular file, and yet some image viewers won't display it, so jpeginfo seems to do a better job – Peter.O Sep 6 '11 at 7:44

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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