Bjoern Olausson

camrename PDF Print E-mail
  
Tuesday, 26 May 2009 19:08

  1. #!/bin/sh
  2. #
  3. # $Id$
  4. #
  5. # Rename a picture taken with the Digital Camera File specification to
  6. # use a more Unix-friendly name.
  7. #
  8. # Usage:
  9. #
  10. USAGE='Usage: camrename [-d] [-v] jpeg-files'
  11. #
  12. # Each given file is renamed.  If the existing name begins or ends in
  13. # four digits (preference given to ending), those four digits will be
  14. # preserved in the new name as "seq"; otherwise "seqn" will be set to
  15. # ensure a unique filename, starting at 0001 or one more than the
  16. # largest existing sequence number.  The remainder of the name will be
  17. # created as follows:
  18. #
  19. #    Pyyyy.mm.dd-hh.mm.ss-seqn.jpg
  20. #     ^^^^ ^^ ^^ ^^ ^^ ^^ ^^^^
  21. #      |   |  |  |  |  |   |
  22. #      |   |  |  |  |  |   +--- Image sequence number
  23. #      |   |  |  |  |  |
  24. #      |   |  |  |  |  +------- Seconds of image creation date/time
  25. #      |   |  |  |  |
  26. #      |   |  |  |  +---------- Minutes of image creation date/time
  27. #      |   |  |  |
  28. #      |   |  |  +------------- Hours of image creation date/time
  29. #      |   |  |
  30. #      |   |  +---------------- Day of month image created
  31. #      |   |
  32. #      |   +------------------- Month image created
  33. #      |
  34. #      +----------------------- Year image created
  35. #
  36. #
  37. # As a side effect, to compensate for MS-DOS stupidity, the execute
  38. # permissions are turned off on all renamed files.
  39. #
  40. # Options:
  41. #
  42. # -d        Debug: report what would be done, but don't do it.
  43. # -v        Verbose: report each rename as it is done.
  44. #
  45.  
  46. # Modified by Bjoern Olausson to work with exiftool instead of Metacam
  47.  
  48. debug=false
  49. verbose=false
  50.  
  51. #
  52. # Argument processing
  53. #
  54. while [ $# -gt 0 ]
  55. do
  56.     case "$1" in
  57.     -d)
  58.         debug=true
  59.         verbose=true
  60.         ;;
  61.     -v)
  62.         verbose=true
  63.         ;;
  64.     --)
  65.         shift
  66.         break
  67.         ;;
  68.     -*)
  69.         echo "$USAGE" 1>&2
  70.         exit 2
  71.         ;;
  72.     *)
  73.         break
  74.         ;;
  75.     esac
  76.     shift
  77. done
  78.  
  79. case "$#" in
  80.     0)
  81.     echo "$USAGE" 1>&2
  82.     exit 2
  83.     ;;
  84. esac
  85.  
  86. #
  87. # Process all file arguments
  88. #
  89. TMP=/tmp/cre$$
  90.  
  91. trap "rm -f $TMP.*; exit 1" 1 2 15
  92. trap "rm -f $TMP.*; exit 0" 13
  93.  
  94. for file ; do
  95.     dir=`dirname $file`
  96.     base=${file%.*}
  97.     origExtension=${file##*.}
  98.     extension=$(tr "A-Z" "a-z" <<< $origExtension)
  99.     #
  100.     # Extract file information with metacam.
  101.     #
  102.     if ! exiftool -TAG -CreateDate "$file" > $TMP.a 2>&1
  103.     then
  104.     echo "Can't extract exif-information from '$file', skipping" 1>&2
  105.     continue
  106.     fi
  107.     datePart=`awk '/Create Date/{print $4" "$5}' $TMP.a | sed -e 's/ /-/;s/:/./g'`
  108.     #
  109.     # Figure out new name, with *lots* of safety checks.
  110.     #
  111.     case "$base" in
  112.     *[0-9][0-9][0-9][0-9])
  113.         seq=`expr "$base" : '.*\([0-9][0-9][0-9][0-9]\)$'`
  114.         ;;
  115.     [0-9][0-9][0-9][0-9]*)
  116.         seq=`expr "$base" : '\([0-9][0-9][0-9][0-9]\).*$'`
  117.         ;;
  118.     *)
  119.         topFile=`ls $dir/P$datePart-*.$extension 2>/dev/null | tail -1`
  120.         case "$topFile" in
  121.         "$dir/P$datePart-*.$extension")
  122.             seq=0001
  123.             ;;
  124.         *)
  125.             seq=`expr "$topFile" \
  126.               : ".*/P$datePart-\([0-9][0-9][0-9][0-9]\).$extension"'$'`
  127.             if [ "X$seq" = X ]
  128.             then
  129.             seq=0001
  130.             fi
  131.             ;;
  132.         esac
  133.         while [ -e "$dir/P$datePart-$seq.$extension" ]
  134.         do
  135.         seq=`echo $seq | awk '{printf "%4.4d
    ", $1 + 1}'
    `
  136.         if  [ $seq -gt 9999 ]
  137.         then
  138.             echo "Can't find unique sequence number to rename $file " \
  139.               "to $dir/P$datePart-*.$extension; not renamed" 1>&2
  140.             continue
  141.         fi
  142.         done
  143.         ;;
  144.     esac
  145.     target="$dir/P$datePart-$seq.$extension"
  146.     if [ "$target" = "$dir/$base.$origExtension" ]
  147.     then
  148.     # Already in proper format
  149.     echo "$base is already in proper format; skipped" 1>&2
  150.     continue
  151.     elif [ -e "$target" ]
  152.     then
  153.     echo "$target already exists; $file not renamed" 1>&2
  154.     continue
  155.     fi
  156.     #
  157.     # Finally, we're ready to do the rename.
  158.     #
  159.     $verbose  &&  echo mv "$file" "$target"
  160.     if ! $debug
  161.     then
  162.     mv "$file" "$target"
  163.     chmod 644 "$target"
  164.     fi
  165. done
  166.  
  167. rm -f $TMP.*
  168.  

 

Add comment


Security code
Refresh

6M Pixel

Banner

www. is deprecated

Banner

Play OGG

Banner