1

I want to delete every RAW image file (Photo image) that has no JPG counterpart.

Here's my current workflow:

  1. My camera creates a RAW and a JPG for every photo I take.
  2. I review the JPG files, and delete the ones I don't want (Since JPGs load much faster)
  3. I currently manually go thought the folder, selecting any RAW file (*.cr3 in my case) that doesn't have a JPG pair (Because I deleted the JPG in the review process).

Example of what the files look like before I review them:

IMG_001.JPG
IMG_001.CR3
IMG_002.JPG
IMG_002.CR3
IMG_003.JPG
IMG_003.CR3
IMG_004.JPG
IMG_004.CR3

Example of what files look like after I've reviewed them

IMG_001.JPG
IMG_001.CR3
IMG_002.CR3
IMG_003.JPG
IMG_003.CR3
IMG_004.CR3

Both IMG_002 and IMG_004 are missing a jpg counterpart, meaning I deleted that jpg counterpart, and now I want to delete the raw file.

I've tried to follow the advice here: and here: but to no avail.

This is my code right now:

for files in RAW/* 
do 
test="JPG/$(basename ${files::9})JPG"
if [ ! -f "$test" ]
then echo "$files"
fi 
done

or, the slick one-line version:

for files in RAW/*; do if [ ! -f "JPG/$(basename ${files::9})JPG" ]; then echo "$files"; fi done

or, the other one liner:

For files in RAW/*; [ ! -f "JPG/$(basename ${files::9})JPG" ] && echo "JPG/$(basename ${files::9})JPG"

The code assumes the raw and jpg files are in separate folders, which is fine. having them in the same folder is slightly more convenient, though.

Basically, I want to know how to write this script. I'm using the terminal on Mac OSX 15 Catalina.

2 Answers2

2

Since the files are in the same directory, let's keep it simple.

#!/bin/sh
for file in *.CR3; do
    noext=${file%.CR3}
    [ -f "$noext.JPG" ] || rm -- "$file"
done

The script loops over all .CR3 files in the current directory. It removes the extension from the filename with noext=${file%.CR3}. If the .JPG counterpart does not exist, the .CR3 file is removed ([ -f "$noext.JPG" ] || rm -- "$file).

You may want to add an echo between || and rm to see if the operation is correct before actually executing the script.

Quasímodo
  • 18,865
  • 4
  • 36
  • 73
0

If your files are as you wrote, you can find all the files that you need by

\ls -1 *.JPG *.CR3 | cut -d. -f1 | uniq -c | sed '/\<2\>/d
s/^[^I]*\(.*\)/\1.CR3/'

and delete them. The complete command will be

rm $(\ls -1 | cut -d. -f1 | uniq -c | sed '/\<2\>/d
s/^[^I]*\(.*\)/\1.CR3/')

Effectively, you are getting the base name of each file by cut, sending it through uniq -c to count, deleting all the files that have 2 instances, and removing 1 and adding the extension CR3.

unxnut
  • 6,008