#!/bin/sh USAGE='Usage: brewmv [options] Rename and move images to the current directory. Assume digital file by default. Depends on exiftool. Copyright © Daniel Ljunggren . Apr 2010 - Apr 2014. [options]: -d : Set DATE. -r : Set film roll number ROLL and assume scanned film image-file -b : Set SEQ_BIAS and add it to index in file name to get count -k : Set count to SEQ_BIAS (ignoring -o) -o : Set SEQ_OFFSET (subtracting SEQ_BIAS by this number) -c : Assume copied film image -v : Assume video-file renaming ' # Rename and move images to the current directory. TYPE="D" # Default serie letter and bias number: SEQ_BIAS=a0000 NOSEQBASE=false SEQ_OFFSET=0 while [ $# -gt 0 ] do case "$1" in -d) if [[ $2 == [0-9][0-9][0-9X][0-9X][0-9X][0-9X] ]]; then DATESET="$2" else echo "$USAGE" 1>&2 exit 2 fi shift 1 ;; -v) TYPE="V" ;; -c) TYPE="C" ;; -r) if [[ $2 == [0-9][0-9][0-9] ]]; then ROLL="$2" [[ ! ${TYPE} == "C" ]] && TYPE="S" else echo "$USAGE" 1>&2 exit 2 fi shift 1 ;; -b) if [[ $2 == [a-z][0-9][0-9][0-9][0-9] ]]; then SEQ_BIAS="$2" else echo "SEQ_BIAS needs a letter and four digits..." echo "$USAGE" 1>&2 exit 2 fi shift 1 ;; -k) NOSEQBASE=true ;; -o) SEQ_OFFSET=$2 shift 1 ;; -*) echo "$USAGE" 1>&2 exit 2 ;; *) break ;; esac shift 1 done if [ $# -eq 0 ]; then echo "$USAGE" 1>&2 exit 2 fi for FILE do if [ -w "$FILE" ]; then case "$FILE" in *.CR2) ORIG_EXT=CR2 NEW_EXT=cr2 ;; *.cr2) ORIG_EXT=cr2 NEW_EXT=cr2 ;; *.CRW) ORIG_EXT=CRW NEW_EXT=crw ;; *.crw) ORIG_EXT=crw NEW_EXT=crw ;; *.RAW) ORIG_EXT=RAW NEW_EXT=raw ;; *.raw) ORIG_EXT=raw NEW_EXT=raw ;; *.jpg) ORIG_EXT=jpg NEW_EXT=jpg ;; *.jpeg) ORIG_EXT=jpeg NEW_EXT=jpg ;; *.JPG) ORIG_EXT=JPG NEW_EXT=jpg ;; *.JPEG) ORIG_EXT=JPEG NEW_EXT=jpg ;; *.avi) ORIG_EXT=avi NEW_EXT=avi ;; *.AVI) ORIG_EXT=AVI NEW_EXT=avi ;; *.tif) ORIG_EXT=tif NEW_EXT=tif ;; *.tiff) ORIG_EXT=tiff NEW_EXT=tif ;; *.TIF) ORIG_EXT=TIF NEW_EXT=tif ;; *.TIFF) ORIG_EXT=TIFF NEW_EXT=tif ;; *.PNG) ORIG_EXT=PNG NEW_EXT=png ;; *.png) ORIG_EXT=png NEW_EXT=png ;; *.MOD) ORIG_EXT=MOD MOI_EXT=MOI NEW_EXT=mod TYPE="V" ;; *.mod) ORIG_EXT=mod MOI_EXT=moi NEW_EXT=mod TYPE="V" ;; *.MOI) ORIG_EXT=MOI MOI_EXT=MOI NEW_EXT=moi TYPE="V" ;; *.moi) ORIG_EXT=moi MOI_EXT=moi NEW_EXT=moi TYPE="V" ;; *.MOV) ORIG_EXT=MOV NEW_EXT=mov TYPE="V" ;; *.mov) ORIG_EXT=mov NEW_EXT=mov TYPE="V" ;; *.MP4) ORIG_EXT=MP4 NEW_EXT=mp4 TYPE="V" ;; *.mp4) ORIG_EXT=mp4 NEW_EXT=mp4 TYPE="V" ;; *) echo "brewmv: Can't handle file '$FILE', skipping" 1>&2 continue ;; esac DIR=`dirname $FILE` BASE=`basename $FILE .$ORIG_EXT` # Find image sequence number if [ $TYPE == "D" ]; then # Digital sensor image TRAIL=`expr "$BASE" : '.*[0-9][0-9][0-9][0-9]\(.*\)$'` SEQ_BASE=`expr "$BASE" : '.*\([0-9][0-9][0-9][0-9]\).*$'` SERIE=`expr "$SEQ_BIAS" : '.*\([a-z]\)[0-9][0-9][0-9][0-9].*$'` SEQ_BIASNR=`expr "$SEQ_BIAS" : '.*\([0-9][0-9][0-9][0-9]\).*$'` if [[ $NOSEQBASE == "true" ]]; then SEQ=${SEQ_BIASNR} else # SEQ_OFFSET should always be smaller than SEQ_BASE SEQ=$(echo "scale=1; ${SEQ_BASE}-${SEQ_OFFSET}+${SEQ_BIASNR}" | bc -l) fi [[ $SEQ == [0-9] ]] && SEQ=000$SEQ [[ $SEQ == [0-9][0-9] ]] && SEQ=00$SEQ [[ $SEQ == [0-9][0-9][0-9] ]] && SEQ=0$SEQ if [[ $SEQ == [0-9][0-9][0-9][0-9][0-9] ]]; then SEQ=`expr $SEQ : '.*\([0-9][0-9][0-9][0-9]\).*$'` # We reached above 9999 so increase SERIE letter case "$SERIE" in a) SERIE="b";; b) SERIE="c";; c) SERIE="d";; d) SERIE="e";; e) SERIE="f";; f) SERIE="g";; g) SERIE="h";; h) SERIE="i";; i) SERIE="j";; j) SERIE="k";; k) SERIE="l";; l) SERIE="m";; m) SERIE="n";; n) SERIE="o";; o) SERIE="p";; p) SERIE="q";; q) SERIE="r";; r) SERIE="s";; s) SERIE="t";; t) SERIE="u";; u) SERIE="v";; v) SERIE="w";; w) SERIE="x";; x) SERIE="y";; y) SERIE="z";; z) echo "brewmv: ERROR: end of alphabet in SERIES numbering!!" 1>&2; exit 1;; esac fi elif [ $TYPE == "S" ] || [ $TYPE == "C" ] ; then # Chemical film image # Workaround for pattern matching returning only the _first_ occuring match of pattern PATTERN="_\([0-9]*\)" MATCH=`echo "${BASE}" | grep -o "${PATTERN}"` NR_BASE=`expr "$MATCH" : "${PATTERN}.*"` LEAD=`echo "$BASE" | sed -e s/"${PATTERN}".*//` TRAIL=`echo "$BASE" | sed -e s/"$LEAD"_"$NR_BASE"//` SEQ_BIASNR=`expr "$SEQ_BIAS" : '.*\([0-9][0-9][0-9][0-9]\).*$'` if [[ $NOSEQBASE == "true" ]]; then NR=${SEQ_BIASNR} else # SEQ_OFFSET should always be smaller than SEQ_BASE NR=$(echo "scale=1; ${NR_BASE}-${SEQ_OFFSET}+${SEQ_BIASNR}" | bc -l) fi # Make sure string is at least two digits long if [ `expr length $NR` == 1 ]; then NR=0$NR fi # Pick only the last two digits if longer LENGTH=`expr length "${NR}"` NR=`echo "${NR}" | cut -b $(( $LENGTH - 1))-$LENGTH` SEQ=$ROLL$NR SERIE="" elif [ $TYPE == "V" ]; then # Video # Decide on type of numbering format SEQ_TEST=`expr "$BASE" : '.*\([a-z][0-9][0-9][0-9][0-9]\).*$'` if [[ -z $SEQ_TEST && ( $NEW_EXT == mod || $NEW_EXT == moi ) ]]; then # Assume 3 digit hex format for MOD and MOI TRAIL=`expr "$BASE" : '.*[0-9][0-9][0-9,a-f,A-F]\(.*\)$'` SEQ_BASE_HEX=`expr "$BASE" : '.*\([0-9][0-9][0-9,a-f,A-F]\).*$'` # Convert hex to dec SEQ_BASE=`echo $((0x$SEQ_BASE_HEX))` else # Assume 4 digit dec format for MOV and others TRAIL=`expr "$BASE" : '.*[0-9][0-9][0-9][0-9]\(.*\)$'` SEQ_BASE=`expr "$BASE" : '.*\([0-9][0-9][0-9][0-9]\).*$'` fi SERIE=`expr "$SEQ_BIAS" : '.*\([a-z]\)[0-9][0-9][0-9][0-9].*$'` SEQ_BIASNR=`expr "$SEQ_BIAS" : '.*\([0-9][0-9][0-9][0-9]\).*$'` if [[ $NOSEQBASE == "true" ]]; then SEQ=${SEQ_BIASNR} else # SEQ_OFFSET should always be smaller than SEQ_BASE SEQ=$(echo "scale=1; ${SEQ_BASE}-${SEQ_OFFSET}+${SEQ_BIASNR}" | bc -l) fi [[ $SEQ == [0-9] ]] && SEQ=000$SEQ [[ $SEQ == [0-9][0-9] ]] && SEQ=00$SEQ [[ $SEQ == [0-9][0-9][0-9] ]] && SEQ=0$SEQ if [[ $SEQ == [0-9][0-9][0-9][0-9][0-9] ]]; then SEQ=`expr $SEQ : '.*\([0-9][0-9][0-9][0-9]\).*$'` # We reached above 9999 so increase SERIE letter case "$SERIE" in a) SERIE="b";; b) SERIE="c";; c) SERIE="d";; d) SERIE="e";; e) SERIE="f";; f) SERIE="g";; g) SERIE="h";; h) SERIE="i";; i) SERIE="j";; j) SERIE="k";; k) SERIE="l";; l) SERIE="m";; m) SERIE="n";; n) SERIE="o";; o) SERIE="p";; p) SERIE="q";; q) SERIE="r";; r) SERIE="s";; s) SERIE="t";; t) SERIE="u";; u) SERIE="v";; v) SERIE="w";; w) SERIE="x";; x) SERIE="y";; y) SERIE="z";; z) echo "brewmv: ERROR: end of alphabet in SERIES numbering!!" 1>&2; exit 1;; esac fi fi # Get the file date information DATE="" if [ $TYPE == "V" ] && [ ! -z $MOI_EXT ]; then MOIFILE="${DIR}/$BASE.$MOI_EXT" # Read from MOI file if [ -e $MOIFILE ]; then YEARHEX1=`od -An -v -tx1 -j0x06 -N1 -w1 $MOIFILE | sed 's/ //g'` YEARHEX2=`od -An -v -tx1 -j0x07 -N1 -w1 $MOIFILE | sed 's/ //g'` # Concatenate strings and convert hex to dec YEARLONG=$((0x$YEARHEX1$YEARHEX2)) YEAR=`echo "${YEARLONG}" | cut -b 3-4` MONTH=`od -An -v -tu1 -j0x08 -N1 -w1 $MOIFILE | sed 's/ //g'` if [[ $MONTH == [0-9] ]]; then MONTH=0$MONTH; fi DAY=`od -An -v -tu1 -j0x09 -N1 -w1 $MOIFILE | sed 's/ //g'` if [[ $DAY == [0-9] ]]; then DAY=0$DAY; fi HOUR=`od -An -v -tu1 -j0x0A -N1 -w1 $MOIFILE | sed 's/ //g'` if [[ $HOUR == [0-9] ]]; then HOUR=0$HOUR; fi MINUTE=`od -An -v -tu1 -j0x0B -N1 -w1 $MOIFILE | sed 's/ //g'` if [[ $MINUTE == [0-9] ]]; then MINUTE=0$MINUTE; fi DATE="$YEAR$MONTH$DAY" fi else # Reads the file EXIF image information: if [ -z $DATESET ]; then DATE=`exiftool -p '$DateTimeOriginal' $FILE | cut -b3-10 | sed 's/ //;s/://g'` else DATE=$DATESET fi fi # Uses the file's date in filename if above information failed: if [ -z $DATE ]; then DATE=`expr "$BASE" : '.*[A-Z]\([0-9][0-9][0-9][0-9][0-9][0-9]\).*$'` if [ -z $DATE ]; then DATE=`expr "$BASE" : '.*[0-9][0-9]\([0-9][0-9][0-9][0-9][0-9][0-9]\)[^0-9].*$'` fi if [ -z $DATE ]; then DATE=`expr "$BASE" : '.*\([0-9][0-9][0-9][0-9][0-9][0-9]\)[^0-9].*$'` fi if [ ! -z $DATE ]; then echo "brewmv: Warning: Using the file's date in filename for $FILE" fi fi # Uses the file's date-stamp if all above date information failed: if [ -z $DATE ]; then DATE=`date -r "$FILE" +%y%m%d` echo "brewmv: Warning: Using the file's date-stamp for $FILE" fi # Create new filename: NEWFILE=${DIR}/$TYPE$DATE\_$SERIE$SEQ"${TRAIL}".$NEW_EXT # Rename file: if [ ! -e ${NEWFILE} ]; then mv $FILE ${NEWFILE} chmod 644 ${NEWFILE} echo "brewmv: $NEWFILE created..." else echo "brewmv: Warning: $NEWFILE already exist, skipping..." fi else echo "brewmv: Warning: $FILE is not writable, skipping..." fi done