This script will print the names of damaged 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 can run it as is or as ./badjpegs > errors.txt to save the output to a file.
To break it down, the find finds *.jpg files in /photos or in any of its subdirectories. These file names are passed to the while loop, which reads them one at a time in the $FILE variable. Inside the loop, we get the result 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.
This can be simplified. Most UNIX commands indicate success or failure in their exit code. If the identify command does this, you can 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 if ! identify; then if ! identify; then , which means that "identified a failure?"
John kugelman
source share