Organize Files by Date Using Bash

I constantly take screenshots of my desktop... or of windows... or anything I'd like to remember at some point in time. I take so many of them, just in case I need them, that they can easily clutter my desktop or Pictures folder. Not sure why I hadn't created it previously, but this lil' snippet of code is a godsend to keep them organized:


ls -A1 | grep -E '[0-9]{4}-[0-9]{2}-[0-9]{2}' | while read -r line; do  
    DNAME="$(echo $line | grep -Eo '[[:digit:]]{4}-[[:digit:]]{2}-[[:digit:]]{2}' | sed 's#-#/#g')"
    mkdir -p "./$DNAME"
    mv "$line" "./$DNAME/"

Basically, any file containing a date in the name (in the yyyy-MM-dd format) is moved into a subdirectory with a similar path (ie ./yyyy/MM/dd or ./2017/01/04/Screenshot...png).

I hope this helps someone. It's also in the ~/.bash_functions file within my dotfiles.

Enjoy! =)