#!/bin/bash # Digital camera photo/video renamer # By Jordon Kalilich (theworldofstuff.com) # GPLv3+ # Run this script after connecting your digital camera. The photos # (.JPG) and videos (.MPG) will be copied to your photos folder, and a # file browser window will open so you can see them. You will be # prompted for a brief description of each file to form part of the # file name. Leave blank to use the previous file's description. # Photos will be moved to $photoPath//. # Videos will be moved to $videoPath. # Photos and videos will be renamed as the following: #
__.ext ############ # Variables: photoPath=~/photos videoPath=~/video cameraPath1=/media/disk/DCIM/101MSDCF cameraPath2=/media/disk-1/DCIM/101MSDCF fileManager="nautilus -g 800x600" ############ # check for existence (actually readability) of local photos folder if [ ! -w "$photoPath" ]; then echo "Local photo folder is not writable" exit fi if [ ! -r "$photoPath" ]; then echo "Local photo folder is not readable" exit fi if [ ! -w "$videoPath" ]; then echo "Local video folder is not writeble" exit fi if [ ! -r "$videoPath" ]; then echo "Local video folder is not readable" exit fi # check for readability of camera folder if [ -r "$cameraPath1" ]; then cameraPath=$cameraPath1 else if [ -r "$cameraPath2" ]; then cameraPath=$cameraPath2 else echo "Neither $cameraPath1 nor $cameraPath2 is readable" exit fi fi # $cameraPath = camera's photos folder # copy photos and videos to main photos folder # I couldn't use -exec with find and I have no idea why echo "Copying files from camera..." for i in `find "$cameraPath" -regex ".*\.[JM]PG"`; do cp "$i" "$photoPath" done # open file manager for easy viewing $fileManager "$photoPath" # do stuff for each photo/video underscore="_" lastNewBase="" for i in `find "$photoPath" -maxdepth 1 -type f -regex ".*\.[JM]PG" | sort`; do # figure out what type of file we've got ext=`echo basename "$i" | awk -F . '{print $NF}'` if [ $ext = "JPG" ]; then isVideo=0 else isVideo=1 fi # get new name base=`basename "$i"` echo -n "Rename $base: " read -a nameWords if [ -n ${nameWords[0]} ]; then newBase=${nameWords[0]} for (( j=1;j<${#nameWords[@]};j++)); do newBase=$newBase$underscore${nameWords[${j}]} done newBase=`echo $newBase | tr -cd "[:alnum:]_" | tr "[:upper:]" "[:lower:]"` # delete funny characters, then convert to lowercase fi if [ -z ${nameWords[0]} ]; then # couldn't use elif or else for some reason newBase=$lastNewBase fi lastNewBase=$newBase # get date of photo and figure out where to put it if [ $isVideo -eq 0 ]; then timestamp=`strings -14 "$i" | egrep -m 1 "^20[0-9][0-9][ :/]?[0-9][0-9][ :/]?[0-9][0-9][ :/]?[0-9][0-9][ :/]?[0-9][0-9][ :/]?[0-9][0-9]"` else timestamp=`stat -c %y "$i"` fi # format: "yyyy:mm:dd hh:mm:ss" year=`echo $timestamp | cut -c1-4` month=`echo $timestamp | cut -c6-7` day=`echo $timestamp | cut -c9-10` if [ $isVideo -eq 0 ]; then destDir=$photoPath/$year/$month mkdir -p $destDir # create directory if it doesn't exist else destDir=$videoPath fi picsFromToday=`find $destDir -maxdepth 1 -type f -name "$year$month$day*" | wc -l` let "picsFromToday += 1" if [ $picsFromToday -lt 10 ]; then # pad digit if necessary picsFromToday=0$picsFromToday fi if [ $picsFromToday -eq 100 ]; then # if > 100 photos, add an extra digit to prev. photos taken today zero="0" for j in `find $destDir -maxdepth 1 -type f -name "*$year$month$day*"`; do allYourBase=`basename $j` allYourBase1=`echo $allYourBase | cut -c1-9` allYourBase2=`echo $allYourBase | cut -c10-` mv $destDir/$allYourBase $destDir/$allYourBase1$zero$allYourBase2 done fi newBase=$year$month$day$underscore$picsFromToday$underscore$newBase.$ext mv $i $destDir/$newBase done