Skip to content

Instantly share code, notes, and snippets.

@hleroy
Created May 13, 2018 21:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hleroy/ef7dd5c7fbc769d73c12fdf56bc4881e to your computer and use it in GitHub Desktop.
Save hleroy/ef7dd5c7fbc769d73c12fdf56bc4881e to your computer and use it in GitHub Desktop.
This script will delete all Canon RAW (CR2) files in a folder when the corresponding JPG is unrated. The purpose is to keep only Raw files for photos which are worth it (assuming you have gone through the effort of rating the JPG). I'm using this script with Digikam in my photo workflow.
#!/bin/bash
#
# This script will delete all Canon RAW (CR2) files in a folder when the corresponding JPG is unrated.
# The purpose is to keep only Raw files for photos which are worth it
# (assuming you have gone through the effort of rating the JPG)
#
# It requires a filename as argument. It is meant to be called through a "context menu"
# (right-click) on a CR2 file.
#
# Example .desktop file for KDE (to be placed in ~/.local/share/applications/myRawCleaner.desktop)
# [Desktop Entry]
# Name=Keep CR2 for rathed photos
# Exec=/usr/bin/konsole -e /bin/bash ~/Scripts/keepRawForRatedPhotos.sh %F # <-- adjust the script location here
# Type=Application
# MimeType=image/x-canon-cr2
# Terminal=false
#
# **WARNING**
# Make sure to be in the right folder before running this script,
# because it will trash all Raw files with no matching rated JPG
#
# Requires:
# exiftool
# trash-cli
# Check that first argument is a file, then extract folder name
if [ -e "$1" ]; then
ALBUM=$(dirname "$1")
else
# Otherwise exit
exit 1
fi
# Check requirements
hash exiftool 2>/dev/null || { echo >&2 "I require exiftool but it's not installed. Aborting."; exit 1; }
hash trash-put 2>/dev/null || { echo >&2 "I require trash-put but it's not installed. Aborting."; exit 1; }
# List all rated JPG file in the folder
cd "$ALBUM"
RATED_JPG=`exiftool -m -if '$Rating' *.JPG -q -p '$FileName'`
if [[ -z "$RATED_JPG" ]]; then
echo "No rated photos"
else
# Create 'Raw' sub-folder if it doesn't exist
if [ ! -d Raw ]; then
mkdir Raw
fi
# Set line break as field separator and loop throuh all rated JPG files
IFS=$'\n'
for JPG in $RATED_JPG
do
CR2=${JPG::-4}'.CR2'
if [ -f "$CR2" ]; then
echo 'Moving '$CR2
mv $CR2 Raw
fi
done
fi
# Trash remaining Raw files in current folder (needs trash-cli package)
trash-put *.CR2
# Wait for a key press before exiting
read -n 1 -s -r -p "Press any key to continue"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment