You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Script to delete sidecar files for missing photo files?

I'm not sure where to post this as this may be an Apple script, or perhaps a terminal script or something else?


I am currently organizing a photo library (outside of Photos) of some 120,000+ photos with some 50,000+ duplicates in it. I'm using Photo Sweeper to help identify the duplicates but when it deletes it only removes the photo file (jpg, heic, raw etc). If the file has a sidecar (aae, xmp) it is left and orphaned.


I'm trying to find a script or a process that will allow me to find and deleted all .xmp and .aae sidecar files in a folder if there is no matching file name without a .xmp or .aae extension (The matching file may be a .jpg, .heic, .raw, .rw2, .png etc)


For the life of me, while I work in scripts all the time for work in Windows, on MacOS I can't figure out how to start with this. I'm thinking this may be an Apple script, or perhaps something UNIX related to run in terminal?


Any suggestions?

iMac Line (2012 and Later)

Posted on Oct 29, 2020 6:15 PM

Reply
Question marked as Top-ranking reply

Posted on Oct 30, 2020 4:08 PM

I hit a roadblock trying to do this in Applescript but I might try again now I figured it out in Bash.


This works in Terminal, if run from the folder I am cleaning up.


#!/bin/bash

for files in {*.aae,*.xmp}

do

filename=${files%.*}

printf "Processing file: %s as %s \n" $files $filename

lines=$(find $filename.* ! '(' -name '*.aae' -o -name '*.xmp' -o -name '.' ')' | wc -l)

printf "Found %s match(es)" $lines

if [ $lines -eq 0 ]

then

Printf "Deleting %s\n" $files

rm $files

fi

done




Similar questions

4 replies
Question marked as Top-ranking reply

Oct 30, 2020 4:08 PM in response to Happy Dad

I hit a roadblock trying to do this in Applescript but I might try again now I figured it out in Bash.


This works in Terminal, if run from the folder I am cleaning up.


#!/bin/bash

for files in {*.aae,*.xmp}

do

filename=${files%.*}

printf "Processing file: %s as %s \n" $files $filename

lines=$(find $filename.* ! '(' -name '*.aae' -o -name '*.xmp' -o -name '.' ')' | wc -l)

printf "Found %s match(es)" $lines

if [ $lines -eq 0 ]

then

Printf "Deleting %s\n" $files

rm $files

fi

done




Oct 30, 2020 10:57 PM in response to Happy Dad

I found bugs in the original solution where it was not handling whitespace correctly and it also what not case insensitive. This new code fixes that and is what I used to solve my issue.


#!/bin/bash

while read sidecarFile

do

Printf "Source: %s\n" "$sidecarFile"

fileName=${sidecarFile%.*}

lines=$(find "$fileName".* ! '(' -type f -iname '*.aae' -o -iname '*.xmp' -o -iname '.' ')' | wc -l)

if [ $lines -eq 0 ]

then

Printf " -- no match found ******** DELETED ********\n"

rm "$sidecarFile"

else

Printf " -- matched\n"

fi

done < <(find "$PWD" -type f -iname '*.aae' -o -iname '*.xmp' | sort -z)


Script to delete sidecar files for missing photo files?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.