The majority of my photo files have the following naming convention / format IMG_20131204_120000.JPG, showing date and time the photo was taken (iPhone/Apple naming convention). However, not all my photos follow this naming convention. With the help of this forum I developed the following script to categorize my photos into date directories:
cd '/photos/' for x in *.JPG *.jpg *.TIF *.tif *.PNG *.png *.BMP *.bmp *.GIF *.gif *.IMG *.img *.JPEG *.jpeg *.TIFF *.tiff; do d=$(date -r "$x" +%Y%m%d)" - Family Photos" t=$(expr substr "$x" 1 4) if [ "$t" == "IMG_" ]; then d=$(expr substr "$x" 5 8)" - Family Photos" fi mkdir -p "$d" >/dev/null 2>&1 mv -- "$x" "$d/" >/dev/null 2>&1 done
PS. I use the IF statement to take the date from the File name itself if the filename is in the format IMG_20131204_120000.JPG as this date most accurately reflects the date the photo was actually taken, but if the file doesn't follow this naming convention I take the operating system date of when the file was last modified/created.
It works fine for the most part except where there are files that start with "IMG_" but then don't conform to the naming convention IMG_20131204_120000.JPG, for example a file called IMG_001.JPG or IMG_Photo1. Is there a better way to structure the IF statement here so that it checks not only that the file starts with "IMG_" but also the next 8 characters are numbers / a date format?