After a photo shoot I have two folders: JPGs (files *.jpg) and RAWs (files *.CR2). Usually I take a look in the JPGs folder and delete those ones that I don't like.

What I want is to create a bash script to check:

 for file.cr2 in folder.getFiles
   if file.jpg is NOT in folder2.getfiles 
      delete file.cr2

I have seen some examples with rsync but with files with the same extension and I'm not very good at bash, so I can do it in C but I want to learn.

  • are you looking for guidance for a C program, or for a command-line / shell-script based solution? – Jeff Schaller Sep 19 '16 at 17:59

Try this:

for file in cr2files/*; do
   test=”jpgfiles/$(basename ${file:: -3})jpg”
   if [ ! –f “$test” ]; then
      echo “$file”

If it gets the results that you want, you can replace the line - echo “$file” - with - rm “$file” -. The line – test=”jpgfiles/$(basename ${file:: -3])jpg” – removes the path and extention from the file name and replaces them with “jpgfile/filename.jpg” . Remember to change the pathnames “cr2files” and “jpgfiles” to the correct ones. You can use variables if you like or pass the path names to your script in arguments.


Your space in “Toshiba ser” was messing up basename. Here’s the solution:

#!/bin /bash
for file in “$1”/*; do
   test=”$2/$(basename ${file:: -3})jpg”
   if [ ! –f “$test” ]; then
      rm “$file”

Call it like this ./removephotos.sh “volumes/toshiba ser/raw” "volumes/toshiba ser/jpg”. Take note that there is nothing between the single quotes in IFS='' on line #2.

| improve this answer | |
  • Hi, Thanks at first. Im doing this: for file in $1; do filename="$(basename {$file)" filename="$2/${filename%.*}.JPG" but when I write something like "removephotos.sh "/volumes/toshiba ser/raw/*" "/volumes/toshiba ser/jpg" it doesn't take the files – Sergiodiaz53 Sep 20 '16 at 16:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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