Shell – Delete all images taken with specific camera

deletefindjpegshellshell-script

What I need:

A command that deletes all .jpg files in the current directory and subdirectories that have been taken with a RaspberryPi Camera.

(Ideally this should work with the Git Bash for Windows, but this would make it just more convenient and is therefore optional.)

What I tried:

I already used find . -name "*.jpg" -size 24k -delete to delete thumbnails and I found out that the file utility is able to display the camera manufacturer (manufacturer=RaspberryPi).

Example:

$ file f49100889.jpg
f49100889.jpg: JPEG image data, Exif standard: [TIFF image data,
big-endian, direntries=10, height=0, manufacturer=RaspberryPi, 
model=RP_OV5647, xresolution=156, yresolution=164, resolutionunit=2, 
datetime=2015:06:09 08:15:03, width=0], baseline, precision 8, 
1920x1080, frames 3

Background:

The filesystem of an NTFS-Drive got corrupted and I restored the files using testdisk/photorec. Now I've got the files but lost some metadata such as the directory structure and file-names. I restored the most important files from a backup already and would like salvage as much of the remaining data as possible. I need to delete some garbage files to make working with the rest easier.

Is it possible to combine these commands to achieve my goal? If yes, how? Is there a better way to go about this?

Thanks in advance!

Best Answer

Here's a simple way. Below are the contents of a script named script.sh in ~/bin/.

#!/usr/bin/env bash
if (file "$1" | grep "RaspberryPi"); then
    echo "rm $1" # <- safety first: just echo, no delete
    # rm "$1" # <- uncomment to delete, leave the quotes
fi

Give it permission to execute (chmod 0750 ~/bin/script.sh). Then find the files and pass results, one at a time (using \;, not +) to the script.

find ~ -iname *.jpg 2>/dev/null -exec ~/bin/script.sh {} \;

Another tool is identify from ImageMagick. This tool describes image characteristics and reads image data, including EXIF. It would be much slower than file, but there's much more information in the image than the information provided by file. It could help when file does not describe the data to be found. Its usage might be as follows.

identify -verbose "$file" | grep "RaspberryPi"

We can test the return code of grep (after using file or identify in the if statement, above) by displaying its return code. From man grep:

Normally the exit status is 0 if a line is selected, 1 if no lines were selected, and 2 if an error occurred.

Combine this information with the knowledge that the $? variable contains the return code/value or exit code/value of the very last command executed.

file image.jpg | grep "RaspberryPi"
echo $?

The lines above would produce a value of zero - match found - for file results that contain the string, "RaspberryPi".

file image.jpg | grep "Rumpelstiltskin"
echo $?

The lines above would produce a value of one - no match - assuming that file does not find a match for the string, "Rumpelstiltskin".

Related Question