Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I check if all files inside directories are valid jpegs (Linux, sh script needed)?

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?

like image 680
Graf Avatar asked Jun 04 '10 12:06

Graf


People also ask

How do you check if any file exists in a directory in shell script?

In order to check if a file exists in Bash using shorter forms, specify the “-f” option in brackets and append the command that you want to run if it succeeds. [[ -f <file> ]] && echo "This file exists!" [ -f <file> ] && echo "This file exists!" [[ -f /etc/passwd ]] && echo "This file exists!"

How do you check if any file exists in a directory in Linux?

In Linux everything is a file. You can use the test command followed by the operator -f to test if a file exists and if it's a regular file. In the same way the test command followed by the operator -d allows to test if a file exists and if it's a directory.

How do I see file extensions in Linux?

To find out file types we can use the file command. Using the -s option we can read the block or character special file. Using -F option will use string as separator instead of “:”. We can use the –extension option to print a slash-separated list of valid extensions for the file type found.


1 Answers

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

like image 129
Alexx Roche Avatar answered Oct 06 '22 23:10

Alexx Roche