0

I have a folder that contains a lot of photos.

I'd like to move them on a more practictal structure, with a folder per day.

The date is extracted from the filename (e.g. from 2016-08-22 16.55.26.jpg to 2016-08-22/2016-08-22 16.55.26.jpg)

What is the best way to do that ?

For the time being, I've created a shell script :

#!/bin/sh
in=~/tmp/rename/*                                                               
out=~/tmp/out                                                                   
for f in $in; do                                                                
    d=$(echo "$f" | grep -Po "\d{4}-\d{2}-\d{2}")                               
    mkdir -p "$out/$d"                                                          
    cp "$f" "$out/$d/"                                                          
done

Is there a better/easy way ? May be with ranger or rename command ?

  • Do the images' filenames already have a datestamp in them (e. g. 2017-05-24-myawesomecat.png)? – DopeGhoti May 23 '17 at 22:04
  • Does the shell script work? Even though the filenames contain spaces? – NickD May 24 '17 at 1:16
  • @nick Yes, because variables are between double quotes. – user1437346 May 24 '17 at 10:23
  • OK - the only thing I can suggest then is a simplified calculation for d: if all the filenames follow the date space time.jpg pattern, you can use the space to split off the desired directory: in bash, you can say d=${f%% *} to pick up everything before the space, but that's bash-specific. In sh you can use d=$(echo $f | cut -d' ' -f1) but whether that's simpler is in the eye of the beholder. – NickD May 24 '17 at 11:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.