Jump to content

Renaming folders based on EXIF


Recommended Posts

I've written a script. Here it is:

#!/bin/bash
PHOTOBASE='/ici/partage/photos'
SORTDIR='a_trier'

function erreur() {
 echo Erreur: $1 >&2
 exit 1
}

cd "$PHOTOBASE" || erreur "Impossible d'aller dans $PHOTOBASE/"
compte=1
while read jpg; do
 read iY iM iD ih im is < <(identify -format "%[EXIF:DateTime]" "$jpg" | sed -r -n 's/^[[:space:]]*([[:digit:]]{4})[^[:digit:]]*([[:digit:]]{2})[^[:digit:]]*([[:digit:]]{2})[^[:digit:]]*([[:digit:]]
{2})[^[:digit:]]*([[:digit:]]{2})[^[:digit:]]*([[:digit:]]{2}).*$/\1 \2 \3 \4 \5 \6/p')
 iDir="${iY}-${iM}"
 iFile="${iY}-${iM}-${iD}-${ih}${im}${is}"
 echo "${iDir}/${iFile} : $jpg (${compte})"
 compte=$(( $compte + 1 ))
 if [ -n "$iDir" ]; then
[ -d "$iDir" ] || mkdir "$iDir"
[ -d "$iDir" ] || erreur "Impossible de créer $iDir/"
suf=
if [ -e "${iDir}/${iFile}.jpg" ] || [ -e "${iDir}/${iFile}_01.jpg" ]; then
  [ -e "${iDir}/${iFile}_01.jpg" ] || mv "${iDir}/${iFile}.jpg" "${iDir}/${iFile}_01.jpg"
  suf=02
  while [ -e "${iDir}/${iFile}_${suf}.jpg" ]; do suf=$(awk '{printf "%02u", $1+1}' <<<"$suf"); done
  suf=_$suf
fi
mv "$jpg" "${iDir}/${iFile}${suf}.jpg"
 fi
done < <(find "${SORTDIR}/" -iname "*.jpg" -type f -print)

PHOTOBASE is a directory that contains a "a_trier" directory (=to_sort), and other directories will be created inside, based on year and month (eg: 200703).

 

Besides, I've written another script, that put the remaining files (videos) in the right folders too:

#!/bin/bash
PHOTOBASE='/ici/partage/photos'
SORTDIR='a_trier'

function erreur() {
 echo Erreur: $1 >&2
 exit 1
}

cd "$PHOTOBASE" || erreur "Impossible d'aller dans $PHOTOBASE/"
compte=1
while read fY fM fD fh fm fs file; do
 fDir="${fY}-${fM}"
 fFile="${fY}-${fM}-${fD}-${fh}${fm}${fs}"
 fExt="$(tr 'A-Z' 'a-z' <<<"${file##*.}")"
 echo "${fDir}/${fFile}.${fExt} : $file (${compte})"
 compte=$(( $compte + 1 ))
 if [ -n "$fDir" ]; then
[ -d "$fDir" ] || mkdir "$fDir"
[ -d "$fDir" ] || erreur "Impossible de créer $fDir/"
suf=
if [ -e "${fDir}/${fFile}.${fExt}" ] || [ -e "${fDir}/${fFile}_01.${fExt}" ]; then
  [ -e "${fDir}/${fFile}_01.${fExt}" ] || mv "${fDir}/${fFile}.${fExt}" "${fDir}/${fFile}_01.${fExt}"
  suf=02
  while [ -e "${fDir}/${fFile}_${suf}.${fExt}" ]; do suf=$(awk '{printf "%02u", $1+1}' <<<"$suf"); done
  suf=_$suf
fi
mv "$file" "${fDir}/${fFile}${suf}.${fExt}"
 fi
done < <(find "${SORTDIR}/" -type f -printf "%TY %Tm %Td %TH %TM %TS %p\n")

 

Both scripts are interruptible anytime with CtrlC. HTH.

 

Yves.

 

[edit:]I've forgotten the most important: how does it answer to your question? Unless I've misunderstood your post, you could move all your current folders into PHOTOBASE/SORTDIR, and they'd get sorted again and the destination folders would be named after the exif data (and photographs too).[/edit]

Link to comment
Share on other sites

Many thanks for posting that :D willl give it a try ...and modify it a bit

 

 

/root/photosort: line 13: syntax error near unexpected token `<'
/root/photosort: line 13: `  read iY iM iD ih im is < < (identify -format "%[EXIF:DateTime]" "$jpg" | sed -r -n 's/^[[:space:]]*([[:digit:]]{4})[^[:digit:]]*([[:digit:]]{2})[^[:digit:]]*([[:digit:]]{2})[^[:digit:]]*([[:digit:]]

#!/bin/bash

PHOTOBASE='/home/sl/100_fuji/test/JPG_FULL'

SORTDIR='a_trier'

 

function erreur() {

echo Erreur: $1 >&2

exit 1

}

 

cd "$PHOTOBASE" || erreur "Impossible d'aller dans $PHOTOBASE/"

compte=1

while read jpg; do

read iY iM iD ih im is < < (identify -format "%[EXIF:DateTime]" "$jpg" | sed -r -n 's/^[[:space:]]*([[:digit:]]{4})[^[:digit:]]*([[:digit:]]{2})[^[:digit:]]*([[:digit

:]]{2})[^[:digit:]]*([[:digit:]]

{2})[^[:digit:]]*([[:digit:]]{2})[^[:digit:]]*([[:digit:]]{2}).*$/\1 \2 \3 \4 \5 \6/p')

iDir="${iY}-${iM}"

iFile="${iY}-${iM}-${iD}-${ih}${im}${is}"

echo "${iDir}/${iFile} : $jpg (${compte})"

compte=$(( $compte + 1 ))

if [ -n "$iDir" ]; then

[ -d "$iDir" ] || mkdir "$iDir"

[ -d "$iDir" ] || erreur "Impossible de créer $iDir/"

suf=

if [ -e "${iDir}/${iFile}.jpg" ] || [ -e "${iDir}/${iFile}_01.jpg" ]; then

[ -e "${iDir}/${iFile}_01.jpg" ] || mv "${iDir}/${iFile}.jpg" "${iDir}/${iFile}_01.jpg"

suf=02

while [ -e "${iDir}/${iFile}_${suf}.jpg" ]; do suf=$(awk '{printf "%02u", $1+1}' <<<"$suf"); done

suf=_$suf

fi

mv "$jpg" "${iDir}/${iFile}${suf}.jpg"

fi

done < <(find "${SORTDIR}/" -iname "*.jpg" -type f -print)

 

hmmm.. and this is still JPEG's ... have still to work out how to get it to work with my raw photo's ...???

 

Anyone spot what I did stupid???

Link to comment
Share on other sites

Hi!

 

I don't know how reliable the CODE snipplet is, but if it reflects your code, then there's an error there: "< (".

The spacing is important: The first '<' is to instruct bash to read the following file as standard input to the command (read). Then there's a space, and then "<(" without spaces, which refers to the system's temporary file that contains the output of the enclosed command.

 

In short, the command inside <(...) (that is: the identify|sed pipe) is executed. In Unix, everything is a file, and the output of this pipe is no exeption; the <(...) construct refers to this file, which is given as a standard input to the "read" command. This is a standard way of doing things in bash to keep the first command (read) from being executed in a subshell.

 

Besides, your "sed" command seems to be all broken...

 

Yves.

Edited by theYinYeti
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...