Recursively search for images, and move them to a date folder Asked a question 9 months ago about how to copy images to a backup folder. That worked in that case! But now I have another problem. I have 233 GB of unsorted data, mostly images, and they are located in a unsorted directory structure.

I was thinking to sort the images based on date (if that is possible on all images), and keep the original filename.

I found a question here, but I haven't tried it - afraid to mess things up.

My question is: how do I recursively search a folder, and move the image of type bmp, jpg, JPG, jpeg and gif to /home/username/Documents/backup/[date] folder (ex. /home/username/Documents/backup/2013-06-10/DSC01158.JPG), and if that folder dosen't exist - create it. And keep the filename, if possible.

System used: macOS Sierra 10.12.4

Edit:
The date has to be extracted from the metadata, because the files dosen´t include date in filename.
Change to the folder you want to search and run

find . -type f -iname '*.jpg' | while read file; do
    ts=$(stat -f '%Sm' -t '%Y-%m-%d' "$file")
    folder="/path/to/backup/folder/$ts"
    [[ -d "$folder" ]] || echo mkdir "$folder"
    echo mv "$file" "$folder/"
done


Best to test this on a small sample first, and remove both echos if the output looks reasonable.