title: Sort files into sub folders by date - bash
body: <p>Basically my HDD crashed, I was able to recover all the files, but, all the files have retained their meta &amp; some have retained their names, I have 274000 images, which I need to more or less, sort into folders by date.</p>
  
  <p>So let's say it starts with the first files, it would get the date from the file, create a sub folder, and until the date changes, keep moving that file into the created folder, once the date changes, it would create a new folder and keep doing the same thing.</p>
  
  <p>I'm sure this is possible, I really didn't want to have to do this manually as it would take weeks...</p>
  
  <p>Lets say I have a target folder /target/</p>
  
  <p>Target contains, 274000 files, in no sub folders at all.</p>
  
  <p>The folders structure should be <strong>/target/YY/DD_MM/filenames</strong></p>
  
  <p>I would like to create a bash script for this, but I'm not really sure where to proceed from here.</p>
  
  <p>I've found this:</p>
  
  <pre><code>#!/bin/bash
  
  DIR=/home/data
  target=$DIR
  cd "$DIR"
  
  for file in *; do
  
      dname="$( date -d "${file%-*}" "+$target/%Y/%b_%m" )"
      mkdir -vp "${dname%/*}"
      mv -vt "$dname" "$file"
  
  done
  </code></pre>
  
  <p>Would creating a folder without checking if it exists delete files inside that folder?</p>
  
  <p>I'm also not quite sure what adding an asterix to the dir pathname would do? </p>
  
  <p>I'm not quite familiar with bash, but I'd love to get this working if someone could please explain to me a little more what's going on?</p>
  
  <p>Thankyou!</p>