#!/bin/bash # # ---------------------------------------------------------------------- # # Description: This script copies graphics files from a # media storage device, card reader, or just any directory, # tries to mount the media, then renames all files to # year-month-day-hour-minutes-seconds-(A/B/C) format, # and creates a year/month/day directory sructure # whereto these files will be copied. Supported graphics # formats are jpg, tif, nef, crw, cr2. The files must # contain an EXIF date-tag, it is used for renaming # the files. # # Author: Maccus # # Initial version: 2004 # # Update: 070509-01 # - The script now also renames corresponding .bib # files from the Bibble RAW conversion software. So if # you have two files, for instance file.cr2 and file.cr2.bib # they will be renamed to Y/M/D/Y-M-D-HM.cr2 # and Y/M/D/Y-M-D-HM.bib # # Update: 070509-02 # - Added choice for copying or moving files. # # Update: 070604-01 # - Changed renaming format of directories to Y-M-D, # instead of Y/M/D # # Update: 080121-01 # - Bugfix: Fixed overwriting of photo's with the # same name. Extension A,B,C... is now added. # # Update: 090110-01 # - Fixed script for Ubuntu. # # Update 090806-01 # - Fixed dependancies checker. # # Update 100601-01 # - Different target directories for photo and video. # # Update 101123-01 # - Fixed unneccesary creation of empty target directories. # # Requires: This script requires certain packages to # ensure correct execution: coreutils, perl-Image-ExifTool # # This script has been written and tested on # Ubuntu Linux 10.04 and Debian Testing (Wheezy) # # ---------------------------------------------------------------------- # # Usage: Place the script in "/usr/local/bin", do a # "chmod +x /usr/local/bin/photo-copy-rename" as root, # and run "photo-copy-rename" from the command line. # # You will be asked to specify the source and destination # folders. The folder-settings are saved in # ~/.photo-rename-copy. You can also specify source and # destination folders as an option: Run # "photo-copy-rename source_folder destination_folder" # from the command line. # # ---------------------------------------------------------------------- # # 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # # ---------------------------------------------------------------------- # # Uncomment for debugging # # set -x # Start script echo -e "\n\033[1;33mSTATUS\033[0m: Script \"$(basename $0)\" is gestart...\n" # Set variables WDIR="${PWD}" SOURCE_DIR="$1" TARGET_DIR_PHOTO="$2" TARGET_DIR_VIDEO="$3" distro_detector () { # Try to find out which type of distro is underneath # Debian, Ubuntu, Mint, Mepis if [ -f "/etc/mepis-release" -o \ -f "/etc/debian_version" ]; then DISTRO="debian" # Redhat, Fedora, Mandriva, PCLinuxOS and others elif [ -f "/etc/tinysofa-release" -o \ -f "/etc/trustix-release" -o \ -f "/etc/whitebox-release" -o \ -f "/etc/yellowdog-release" -o \ -f "/etc/pclinuxos-release" -o \ -f "/etc/mandriva-release" -o \ -f "/etc/redhat-release" -o \ -f "/etc/redhat_version" -o \ -f "/etc/fedora-release" ]; then DISTRO="redhat" # SuSE elif [ -f "/etc/SuSE-release" -o \ -f "/etc/novell-release" -o \ -f "/etc/sles-release" ]; then DISTRO="suse" else DISTRO="unknown" fi } distro_detector # Check to see if the required package are installed if [ "${DISTRO}" = "redhat" ]; then for PACKAGE in coreutils perl-Image-ExifTool ; do if [ "${PACKAGE}" = "perl-Image-Exiftool" ]; then if ! rpm -q ${PACKAGE} > /dev/null ; then if ! rpm -q Image-Exiftool > /dev/null ; then echo -e "Script will not run without \"perl-Image-Exiftool\" or \"Image-Exiftool\"" echo -e "Please install \"perl-Image-Exiftool\" or \"Image-Exiftool\"" echo -e "Aborting now..." sleep 5 exit 1 fi fi elif ! rpm -q ${PACKAGE} > /dev/null ; then echo -e "Script will not run without \"${PACKAGE}\"" echo -e "Please install\"${PACKAGE}\"" echo -e "Aborting now..." sleep 5 exit 1 fi done elif [ "${DISTRO}" = "debian" ]; then for PACKAGE in libimage-exiftool-perl ; do if ! dpkg -l | grep -q "^ii ${PACKAGE} " ; then echo -e "Script will not run without \"${PACKAGE}\"" echo -e "Please install\"${PACKAGE}\"" echo -e "Aborting now..." sleep 5 exit 1 fi done else echo "Only Mandriva and Ubuntu supported by this script..." sleep 5 exit 1 fi # If we did not specify source and destination on the commandline, do it now if [ -z "$1" -o -z "$2" -o -z "$3" ]; then if [ ! -f "${HOME}/.photo-copy-rename" ]; then echo -en "Copy or the files or move the files? Type \"copy\" or type \"move\", or hit Enter for [copy]: " read COPY_MOVE COPY_MOVE="${COPY_MOVE:=copy}" echo -en "Specify the source folder, or hit enter for [$PWD]: " read SOURCE_DIR SOURCE_DIR="${SOURCE_DIR:=$PWD}" echo -en "Specify the destination folder for PHOTOS, or hit enter for [$PWD/renamed]. If the folder does not exist it will be created: " read TARGET_DIR_PHOTO TARGET_DIR="${TARGET_DIR_PHOTO:=$PWD/renamed}" echo -en "Specify the destination folder for VIDEOS, or hit enter for [$PWD/renamed]. If the folder does not exist it will be created: " read TARGET_DIR_VIDEO TARGET_DIR="${TARGET_DIR_VIDEO:=$PWD/renamed}" else . "${HOME}/.photo-copy-rename" TMP_COPY_MOVE="${COPY_MOVE}" echo -en "Copy or the files or move the files? Type \"copy\" or type \"move\", or hit Enter for [${TMP_COPY_MOVE}]: " read COPY_MOVE COPY_MOVE="${COPY_MOVE:=$TMP_COPY_MOVE}" TMP_SOURCE_DIR="${SOURCE_DIR}" echo -en "Specify the source folder, or hit Enter for [${TMP_SOURCE_DIR}]: " read SOURCE_DIR SOURCE_DIR="${SOURCE_DIR:=$TMP_SOURCE_DIR}" TMP_TARGET_DIR_PHOTO="${TARGET_DIR_PHOTO}" echo -en "Specify the destination folder for PHOTOS, or hit Enter for [${TMP_TARGET_DIR_PHOTO}]: " read TARGET_DIR_PHOTO TARGET_DIR_PHOTO="${TARGET_DIR_PHOTO:=$TMP_TARGET_DIR_PHOTO}" TMP_TARGET_DIR_VIDEO="${TARGET_DIR_VIDEO}" echo -en "Specify the destination folder for VIDEOS, or hit Enter for [${TMP_TARGET_DIR_VIDEO}]: " read TARGET_DIR_VIDEO TARGET_DIR_VIDEO="${TARGET_DIR_VIDEO:=$TMP_TARGET_DIR_VIDEO}" fi fi # If COPY_MOVE is empty, always set copy to be on the safe side... COPY_MOVE="${COPY_MOVE:=copy}" # Set copy or move command case "${COPY_MOVE}" in copy) CPMV="cp";; move) CPMV="mv";; *) CPMV="cp";; esac # Write config file echo -e "COPY_MOVE=\"${COPY_MOVE}\"\nSOURCE_DIR=\"${SOURCE_DIR}\"\nTARGET_DIR_PHOTO=\"${TARGET_DIR_PHOTO}\"\nTARGET_DIR_VIDEO=\"${TARGET_DIR_VIDEO}\"\n" > "${HOME}/.photo-copy-rename" # Check if source folder exists if [ -d "${SOURCE_DIR}" ] ; then # Mount media (i.e. a card reader) if grep -q "${SOURCE_DIR}" "/etc/mtab" ; then cd "${SOURCE_DIR}" cd "${WDIR}" else mount "${SOURCE_DIR}" && echo -e "\nMounted \"${SOURCE_DIR}\"\n" sleep 1 cd "${SOURCE_DIR}" cd "${WDIR}" fi # Find graphics files on media find "${SOURCE_DIR}" -type f \( -iname '*\.jpg' \ -o -iname '*\.jpeg' \ -o -iname '*\.cr2' \ -o -iname '*\.crw' \ -o -iname '*\.nef' \ -o -iname '*\.tif' \ -o -iname '*\.tiff' \ -o -iname '*\.mov' \ \) | sort -d | \ while read FILE ; do DIRNAME="`dirname "${FILE}"`" echo "${FILE}" | egrep -q "\.jpg$" && EXT="jpg" echo "${FILE}" | egrep -q "\.JPG$" && EXT="JPG" echo "${FILE}" | egrep -q "\.jpeg$" && EXT="jpeg" echo "${FILE}" | egrep -q "\.JPEG$" && EXT="JPEG" echo "${FILE}" | egrep -q "\.cr2$" && EXT="cr2" echo "${FILE}" | egrep -q "\.CR2$" && EXT="CR2" echo "${FILE}" | egrep -q "\.crw$" && EXT="crw" echo "${FILE}" | egrep -q "\.CRW$" && EXT="CRW" echo "${FILE}" | egrep -q "\.nef$" && EXT="nef" echo "${FILE}" | egrep -q "\.NEF$" && EXT="NEF" echo "${FILE}" | egrep -q "\.tif$" && EXT="tif" echo "${FILE}" | egrep -q "\.TIF$" && EXT="TIF" echo "${FILE}" | egrep -q "\.tiff$" && EXT="tiff" echo "${FILE}" | egrep -q "\.TIFF$" && EXT="TIFF" echo "${FILE}" | egrep -q "\.mov$" && EXT="mov" echo "${FILE}" | egrep -q "\.MOV$" && EXT="MOV" case "${EXT}" in jpg) BASENAME="`basename "${FILE}" .jpg`";; JPG) BASENAME="`basename "${FILE}" .JPG`";; jpeg) BASENAME="`basename "${FILE}" .jpeg`";; JPEG) BASENAME="`basename "${FILE}" .JPEG`";; cr2) BASENAME="`basename "${FILE}" .cr2`";; CR2) BASENAME="`basename "${FILE}" .CR2`";; crw) BASENAME="`basename "${FILE}" .crw`";; CRW) BASENAME="`basename "${FILE}" .CRW`";; nef) BASENAME="`basename "${FILE}" .nef`";; NEF) BASENAME="`basename "${FILE}" .NEF`";; tif) BASENAME="`basename "${FILE}" .tif`";; TIF) BASENAME="`basename "${FILE}" .TIF`";; tiff) BASENAME="`basename "${FILE}" .tiff`";; TIFF) BASENAME="`basename "${FILE}" .TIFF`";; mov) BASENAME="`basename "${FILE}" .mov`";; MOV) BASENAME="`basename "${FILE}" .MOV`";; esac EXT_LOWERCASE="`echo ${EXT} | tr 'A-Z' 'a-z'`" if [ "${EXT}" = "mov" -o "${EXT}" = "MOV" ]; then DATE_STRING="`exiftool -CreateDate "${FILE}" 2> /dev/null`" else DATE_STRING="`exiftool -DateTimeOriginal "${FILE}" 2> /dev/null`" fi # This is for scanned graphics files [ -z "${DATE_STRING}" ] && DATE_STRING="`exiftool "${FILE}" | grep "Date/Time Of Last Modification"`" # Set variables for renaming YEAR_DIR="`echo ${DATE_STRING} | cut -d ":" -f 2 | sed "s|[ ]||g"`" MONTH_DIR="`echo ${DATE_STRING} | cut -d ":" -f 3`" DAY_DIR="`echo ${DATE_STRING} | cut -d ":" -f 4 | sed "s|[ ].*||"`" TIME_DIR="`echo ${DATE_STRING} | cut -d ":" -f 4,5,6 | sed "s|.*[ ]||" | sed "s|[:]||g"`" case ${MONTH_DIR} in Jan) MONTH_DIR="01";; Feb) MONTH_DIR="02";; Mar) MONTH_DIR="03";; Apr) MONTH_DIR="04";; May) MONTH_DIR="05";; Jun) MONTH_DIR="06";; Jul) MONTH_DIR="07";; Aug) MONTH_DIR="08";; Sep) MONTH_DIR="09";; Oct) MONTH_DIR="10";; Nov) MONTH_DIR="11";; Dec) MONTH_DIR="12";; esac [ "${DAY_DIR}" -lt 10 ] && DAY_DIR=0${DAY_DIR} DAY_DIR="`echo "${DAY_DIR}" | sed "s|00|0|"`" if [ "${EXT}" = "mov" -o "${EXT}" = "MOV" ]; then [ -d "${TARGET_DIR_VIDEO}/${YEAR_DIR}-${MONTH_DIR}-${DAY_DIR}" ] || mkdir -p "${TARGET_DIR_VIDEO}/${YEAR_DIR}-${MONTH_DIR}-${DAY_DIR}" TARGET="${TARGET_DIR_VIDEO}/${YEAR_DIR}-${MONTH_DIR}-${DAY_DIR}/${YEAR_DIR}-${MONTH_DIR}-${DAY_DIR}-${TIME_DIR}" else [ -d "${TARGET_DIR_PHOTO}/${YEAR_DIR}-${MONTH_DIR}-${DAY_DIR}" ] || mkdir -p "${TARGET_DIR_PHOTO}/${YEAR_DIR}-${MONTH_DIR}-${DAY_DIR}" TARGET="${TARGET_DIR_PHOTO}/${YEAR_DIR}-${MONTH_DIR}-${DAY_DIR}/${YEAR_DIR}-${MONTH_DIR}-${DAY_DIR}-${TIME_DIR}" fi # Rename graphics files and copy them to destination folder for COUNT in A B C D E F G ; do if [ ! -f "${TARGET}-${COUNT}.${EXT_LOWERCASE}" ]; then ${CPMV} -v "${DIRNAME}/${BASENAME}.${EXT}" "${TARGET}-${COUNT}.${EXT_LOWERCASE}" chmod -x "${TARGET}-${COUNT}.${EXT_LOWERCASE}" 2> /dev/null if [ -f "${DIRNAME}/${BASENAME}.${EXT}.bib" ]; then ${CPMV} -f "${DIRNAME}/${BASENAME}.${EXT}.bib" "${TARGET}-${COUNT}.${EXT_LOWERCASE}.bib" fi break fi done done # Unmount media if grep -q "${SOURCE_DIR}" "/etc/mtab" ; then cd "${WDIR}" sleep 1 umount "${SOURCE_DIR}" && echo -e "\nUnmounted \"${SOURCE_DIR}\"" fi else echo -e "\nWarning! \"${SOURCE_DIR}\" not found! Could not copy files." fi echo -e "\nDone...\nHit Enter to continue..." read exit 0