Here's my take on this issue. A lot of good ideas came from earlier scripts mentioned here.
This is a bash script for OS X. It looks for files that exist with same base filename and dng+jpg
extensions. If a jpg
is found with an exactly same name as dng
, then that filename is displayed (-e
), file is moved (-m
) or deleted (-d
).
It will go through subfolders, so you could use it for your entire catalog or just parts of it.
For other raw file extensions just substitute *.dng
in the script with your preferred extension.
Warning: You could have two different images with same name, but different extension. Those are inevitable casualties of this script.
Here's how to use the script:
Usage: dng-jpg.sh [-m <path>] [-d <path>] [-e <path>] [-h]
-m: for move (moves files to <path>/duplicates)
-d: for delete (deletes duplicate files)
-e: for echo (lists duplicate files)
-h: for help
Basic usage would work like this:
$ ./dng-jpg.sh -e /Volumes/photo/DNG/2015
That would echo all filenames of jpg
files that match the criteria of having both dng
and jpg
file with same name.
Result would look like something like this:
Echo selected with path: /Volumes/photo/DNG/2015
/Volumes/photo/DNG/2015/03/18/2015-03-18_02-11-17.jpg
/Volumes/photo/DNG/2015/06/01/2015-06-01_05-10-50.jpg
/Volumes/photo/DNG/2015/06/01/2015-06-01_05-10-56.jpg
/Volumes/photo/DNG/2015/06/01/2015-06-01_05-11-39.jpg
/Volumes/photo/DNG/2015/06/01/2015-06-01_05-11-54.jpg
/Volumes/photo/DNG/2015/06/01/2015-06-01_05-12-26.jpg
/Volumes/photo/DNG/2015/06/01/2015-06-01_05-12-43.jpg
/Volumes/photo/DNG/2015/06/01/2015-06-01_05-13-21.jpg
/Volumes/photo/DNG/2015/06/01/2015-06-01_05-13-56.jpg
9 files found.
Now if I want to delete the files I would just switch the -e
to -d
:
$ ./dng-jpg.sh -d /Volumes/photo/DNG/2015
Or if I'd like to move the files to /duplicates I'd execute it with -m
.
$ ./dng-jpg.sh -m /Volumes/photo/DNG/2015
Now the duplicate jpg
files would be in /Volumes/photo/DNG/2015/duplicates
Here's the script: dng-jpg.sh
#!/bin/bash
# Init variables
isSetM=0
isSetD=0
isSetE=0
isSetCount=0
counter=0
#Display usage info
usage() {
cat <<EOF
Usage: dng-jpg.sh [-m <path>] [-d <path>] [-e <path>] [-h]
-m: for move (moves files to <path>/duplicates)
-d: for delete (deletes duplicate files)
-e: for echo (lists duplicate files)
-h: for help
EOF
exit 1
}
#Check for parameters
while getopts ":m:d:e:h" opt; do
case ${opt} in
m)
isSetM=1
let isSetCount="$isSetCount+1"
arg=${OPTARG}
echo "Move selected with path:" $arg
;;
d)
isSetD=1
let isSetCount="$isSetCount+1"
arg=${OPTARG}
echo "Delete selected with path:" $arg
;;
e)
isSetE=1
let isSetCount="$isSetCount+1"
arg=${OPTARG}
echo "Echo selected with path:" $arg
;;
h)
let isSetCount="$isSetCount+1"
usage
;;
\?)
echo "Invalid option: -$OPTARG" >&2
usage
;;
:)
echo "Option -$OPTARG requires a directory argument." >&2
usage
;;
*)
usage
;;
esac
done
# If no parameters, show usage help and exit
if test -z "$1"; then
usage
fi
# If multiple parameters (not counting -a), show usage help and exit
if (($isSetCount > 1)); then
usage
fi
#Verify directory
if [ ! -d "$arg" ]; then
echo "$arg is not a path to a directory." >&2
usage
fi
#Now set it as a basedir
BASEDIR=$arg
WASTEDIR="$BASEDIR/duplicates/"
if (( $isSetM==1 )); then
mkdir $WASTEDIR
fi
for filename in $(find $BASEDIR -name '*.dng' -exec echo {} \; | sort); do
prefix=${filename%.dng}
if [ -e "$prefix.jpg" ]; then
let counter="$counter+1"
if (( $isSetE==1 )); then
echo "$prefix.jpg"
fi
if (( $isSetM==1 )); then
mv $prefix.jpg $WASTEDIR
fi
if (( $isSetD==1 )); then
rm $prefix.jpg
fi
fi
done
echo "$counter files found."