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.

  • 1
    I assume the date is to be taken from the metadata of the file (aka the timestamp it has)? The example in the second link won't really help you then, there the date is taken from the filename.
    – nohillside
    Apr 10, 2017 at 14:36
  • Aha alright! But yes, that is correct. The timestamp is from the metadata, not the filename itself.
    – Adam
    Apr 10, 2017 at 14:38
  • Ah, did only look at the accepted answer there. The others actually do more or less what you need as well
    – nohillside
    Apr 10, 2017 at 15:22

1 Answer 1


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")
    [[ -d "$folder" ]] || echo mkdir "$folder"
    echo mv "$file" "$folder/"

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

  • Would changing "mv" in the above command to "cp" allow you to copy as opposed to moving the files to a new directory? What does "$ts" mean?
    – user26732
    Apr 17, 2017 at 13:50
  • @user26732 Yes, cp will copy the files instead of moving them, ts is the variable assigned in the line before (ts=$(...)). Run the part inside ( ) to see what it does.
    – nohillside
    Apr 17, 2017 at 14:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .