--- /dev/null
+[[name-by-date.sh]] is a [[bash]] script to rename JPEGs to show their
+creation date.
+
+ $ name-by-date.sh *.JPG
+ DSC_0350.JPG taken on "2010.07.02.08.36.20"
+ DSC_0356.JPG taken on "2010.07.02.08.38.10"
+ ...
+
+`name-by-date.sh` uses Daniel Stephens' [MetaCam][] (ebuild in my
+[[Gentoo overlay]]) to extract thed ate from the picture's metadata,
+so you'll have to install that first.
+
+[metacam]: http://www.cheeseplant.org/~daniel/pages/metacam.html
+
+[[!tag tags/bash]]
+[[!tag tags/programming]]
--- /dev/null
+#!/bin/bash
+#
+# Rename JPEGs to reflect the creation date stored in their metadata.
+#
+# Copyright (C) 2010 W. Trevor King <wking@drexel.edu>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with this program; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+#
+#
+# usage: name-by-date.sh $(find . -name '*.jpg')
+#
+# uses metacam, [, cut, dirname, echo, file, grep, let, mv, and sed.
+
+
+while [ -n "$1" ]; do
+ FILE=$1
+ TYPE=$(file "$FILE" | grep JPEG)
+ if [ -n "$TYPE" ]; then
+ DATE_LINE=$(metacam "$FILE" | grep 'Image Creation Date')
+ DATE=$(echo "$DATE_LINE" | cut -d: -f 2,3,4,5,6 | sed 's/:/./g' | cut -d\ -f2,3 | sed 's/ /./g')
+ if [ -z "$DATE" ]; then
+ echo "$FILE has no stored date"
+ else
+ echo "$FILE taken on \"$DATE\""
+ FILE_DIR=$(dirname "$FILE")
+ NEW_NAME="$FILE_DIR/$DATE.jpg"
+ if [ -e "$NEW_NAME" ] && [ "$NEW_NAME" != "$FILE" ]; then
+ i=1
+ NEW_NAME="$FILE_DIR/${DATE}_$i.jpg"
+ while [ -e "$NEW_NAME" ] && [ "$NEW_NAME" != "$FILE" ]; do
+ echo "File \"$NEW_NAME\" already exists!"
+ NEW_NAME="$FILE_DIR/${DATE}_$i.jpg"
+ let "i += 1"
+ done
+ fi
+ if [ "$NEW_NAME" != "$FILE" ]; then
+ mv "$FILE" "$NEW_NAME"
+ fi
+ fi
+ fi
+ shift
+done