这是我对这个问题的看法。许多好的想法来自这里提到的早期脚本。
这是OS X的bash脚本。它查找具有相同基本文件名和dng+jpg
扩展名的文件。如果jpg
找到的名称与完全相同dng
,则显示(-e
)该文件名,将移动(-m
)或删除(-d
)文件。
它会通过子文件夹,因此您可以将其用于整个目录或部分目录。
对于其他原始文件扩展名,只需*.dng
在脚本中替换您首选的扩展名即可。
警告:您可能有两个名称相同但扩展名不同的不同图像。这些是该脚本不可避免的伤亡。
这是使用脚本的方法:
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
基本用法如下所示:
$ ./dng-jpg.sh -e /Volumes/photo/DNG/2015
这将回jpg
显符合具有dng
和jpg
具有相同名称的文件的条件的文件的所有文件名。
结果看起来像这样:
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.
现在,如果要删除文件,只需将切换-e
到-d
:
$ ./dng-jpg.sh -d /Volumes/photo/DNG/2015
或者,如果我想将文件移至/ duplicates,则可以执行-m
。
$ ./dng-jpg.sh -m /Volumes/photo/DNG/2015
现在重复的jpg
文件将在/Volumes/photo/DNG/2015/duplicates
这是脚本: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."