christian@sugarpie:: ./jeje.sh -r hehelol [EXIFLOL]
Recursive set.
Copying file hehelol/IMG_2478.JPG to /media/bilder/2005/10/25/
Copying file hehelol/IMG_2480.JPG to /media/bilder/2005/10/25/
Copying file hehelol/IMG_2481.JPG to /media/bilder/2005/10/28/
Copying file hehelol/IMG_2482.JPG to /media/bilder/2005/10/28/
Copying file hehelol/IMG_2483.JPG to /media/bilder/2005/10/30/
Copying file hehelol/IMG_2484.JPG to /media/bilder/2005/10/30/
Copying file hehelol/IMG_2485.JPG to /media/bilder/2005/10/30/
Copying file hehelol/IMG_2486.JPG to /media/bilder/2005/10/30/
[...]
spug wanted a script for sorting his piktars based on EXIF-data. I made one. It's not done yet, but it basically works, and creates a directory stucture based on year, month and date as seen above. The recursive flag is just for show as of now - the script will act recursively even if you don't tell it too. You can provide as many directories and "loose" files as you wish, it will check whether it's a regular file(-f) or a directory(-d) and put it in the desired array. It will skip files where $year, $month, and $day can't be grepped(-z), which means files without valid EXIF-data containing these values won't be copied.
Here's the script so far. It's not done, it needs cleaning up as well as some improvements. TODO: Fix recursive behaviour, really implement --delete(move files instead of copying them) ++. Verbose mode. I should set cp/mv to interactive mode as well, I guess. Yes, here goes. Any comments so far?
$ cat jeje.sh | grep -Ev "#[a-z_A-Z]"
#!/bin/bash
imagepath="/media/bilder"
if [ $# -eq 0 ]; then
echo "No file specified!"
exit 1
fi
for i in "$@"; do
case $i in
--recursive|-r)
recursive="1"
;;
--delete|-d)
delete_files="1"
;;
--version|-v)
verbose="1"
;;
esac
if test -d $i; then
DirArray=( "${DirArray[@]}" "$i" )
elif test -f $i; then
FileArray=( "${FileArray[@]}" "$i" )
# if test -n $verbose; then
# echo "$i not a valid file or directory!"
# fi
#
fi
done
if test -n "$recursive"; then
echo "Recursive set."
fi
if test -n "$delete_files"; then
echo "Will move files instead of copying them."
fi
if test -n "$verbose"; then
echo "Verbose set."
fi
function MoveFile {
if [ -f "$1" ]; then
date=`exif $1 | grep Date | sed 1q |cut -d"|" -f2 | cut -d" " -f1`
year=`echo $date|cut -d":" -f1`
month=`echo $date|cut -d":" -f2`
day=`echo $date|cut -d":" -f3`
newpath="$imagepath/$year/$month/$day/"
if [[ -z $year ]] || [[ -z $month ]] || [[ -z $day ]]; then
echo "Missing EXIF-data, skipping file $1"
else
mkdir -p "$newpath"
echo "Copying file $1 to $newpath"
cp -p "$1" "$newpath"
fi
else
echo "Not a valid file, skipping $1"
fi
}
for i in $DirArray
do
for x in `find $i | grep -Ev "^.$|^..$|^$i$"`
do
MoveFile $x
done
done
for i in $FileArray
do
MoveFile $i
done
exit 0
Yes, I'm lazy.
Edit: Updated script
here 8-)