Parsing folder for pairs with bash or AppleScript

applescriptbashfinderterminal

I edit RAW photos in Photoshop using Adobe Camera Raw. The way the edits work is any time I work on a picture, Photoshop creates a text file with the same name as the picture to which the edits were made. However, sometimes when I delete an image, or use it in a Photomerge, the XML file stays behind. Thus I now have a folder with ~1100 RAW images (.NEF), ~1300 text files (.xmp) and a few photoshop files (.psd, .psb).

I would like to parse this folder, using either AppleScript or a bash command, and either mark or delete the .xmp files that do not have a companion RAW file. Logically, the code would be: for every .xmp file, check for a .NEF file with same name. If .NEF does not exist, delete or mark the .xmp file. If .NEF does exist, do nothing.

I am not familiar with AppleScript, and although I am familiar with ffmpeg, diskutil, and basic cd/ls and rsync commands in terminal, I have never attempted something like this and do not know where to start.

So, I was hoping someone on here could:

  1. Help decide whether bash or AppleScript is more appropriate here AND
  2. Help me write the sequence OR point me to some good resources where I could learn enough (either bash or AppleScript) to write the sequence myself.

Best Answer

In Terminal, change directory, e.g. cd /path/to/files, to the location of the target files and then use the following command to delete any .xmp file that doesn't have a corresponding .NEF file:

for f in *.xmp; do [[ ! -f ${f%.*}.NEF ]] && rm "$f"; done