1

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.

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255

1 Answers1

0

Try this:

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

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.

Edit:

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

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

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.

bashBedlam
  • 1,049
  • 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