Skip to content

Instantly share code, notes, and snippets.

@mendhak
Last active April 28, 2024 20:58
Show Gist options
  • Save mendhak/14e840dfdede3aa716fbe4b454897c9b to your computer and use it in GitHub Desktop.
Save mendhak/14e840dfdede3aa716fbe4b454897c9b to your computer and use it in GitHub Desktop.
Script to delete XMPs and other sidecar files in a specified directory, if the base image file does not exist
#!/bin/bash
# modified from # https://stackoverflow.com/questions/48757748/removing-orphaned-sidecar-files-by-extension
# Check if directory is provided as argument
if [ $# -ne 1 ]; then
echo "Usage: $0 directory_path"
exit 1
fi
directory="$1"
# Check if the provided directory exists
if [ ! -d "$directory" ]; then
echo "Error: Directory '$directory' does not exist."
exit 1
fi
# Change to the specified directory
cd "$directory" || exit 1
shopt -s nullglob extglob nocaseglob;
# Get all sidecar files
for file in *.{xmp,pts,pp3,dop}
do
# Generate all permutations of filenames that it may belong to,
# and let globbing delete the ones that don't exist
candidates=("${file%.*}"@() "${file%%.*}".{jpg,jpeg,arw,on1,onphoto,raw,nef,raf,orf}@()); # add possible extension types that may be present here
# echo $candidates
# If none exist, the file can be deleted
[[ ${#candidates[@]} -eq 0 ]] && echo "Found orphan $file" # && rm -f $file # uncomment this to actually delete the file
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment