#!/bin/bash # Script to prepare photos for Facebook or similar, # by Richard H. Tingstad (richard.tingstad+fb@gmail.com) # # Makes a directory ('facebook') # Copies all jp(e)g files there # Gives copied files names like YYYYMMDDHHMMSS-C.jpg # Prompts user for further action # Resizes all copy images to max 720px width/height # and removes metadata # Renames all copied files to 00000.jpg,00001.jpg,... dir='facebook' echo "Creating directory '$dir'..." if ! mkdir "$dir" then exit 1 fi c=0 find . -maxdepth 1 -path ./$dir -prune -o \( -iname '*.jpg' -or -iname '*.jpeg' \) | while read i do if [ ! -f "$i" ] then echo "Skipping non-regular file '$i'" continue fi t=`identify -verbose "$i" |egrep -m 1 '[Ee]xif:DateTime'|sed 's/[^0-9]//g'` if [ "$t" = "" ] then y=`stat -c %y "$i" | sed 's/[^0-9]//g'` z=`stat -c %z "$i" | sed 's/[^0-9]//g'` if [[ "$y" < "$z" ]] then t="$y" else t="$z" fi if echo $t | egrep -q '[0-9]{15,}' then t=`echo $t | egrep -o '^[0-9]{14}'` fi fi dest="./$dir/$t-$c.jpg" echo "$i > $dest" cp "$i" "$dest" c=$[ $c + 1 ] done echo "Files have been copied to directory '$dir' and renamed (sorted) according to creation time." echo "Do you also want to resize them, strip them of metadata, and give them anonymous filenames [Y/n]?" read a if [ "$a" == "n" ] then exit 0 fi cd $dir #Resize photos and remove metadata. find . -name '*.jpg' | while read i do echo "Resizing and deleting metadata from $i" mogrify -strip -resize 720x720 $i done #Anonymize file names. c=0 for i in `ls *.jpg` do f=`printf "%05u" "$c"` echo "$i > $f.jpg" mv $i $f.jpg c=$[ $c + 1 ] done cd ..