title: Use datestring in a filename to create folder directory and move files
body: <p>The script I'm trying to pull of should move files to a destination folder and place them in "year/month/" folders according to the files name which starts with YYYY-MM-DD.</p>
  
  <p>Example:</p>
  
  <pre><code>2013-08-03-image_name.png -&gt; ~/B/uploads/2013/08/2013-08-03-image_name.png
  2012-01-01-image_name.png -&gt; ~/B/uploads/2012/01/2012-01-01-image_name.png
  </code></pre>
  
  <h2>Plan of action</h2>
  
  <p><strong>(1)</strong> Set path variables</p>
  
  <pre><code>source=~/Desktop/A/
  targetPath=~/Desktop/B/uploads/
  </code></pre>
  
  <p><strong>(2)</strong> Perform these actions on each file in <code>$source</code></p>
  
  <pre><code>cd "$source";
  for i in *.png
  do
      # STEP 3
      # STEP 4
  done
  </code></pre>
  
  <p><strong>(3)</strong> Step 3: Image Optimization √</p>
  
  <p><strong>(4)</strong> Step 4: File away files to directory that machtes datename</p>
  
  <p><strong>(4a)</strong> Search for datestring in filename via  <code>^(\d{4})-(\d{2})</code> and create <code>$datePath</code>, c.f. <code>datePath=2013/08/</code>. I image this something like this…</p>
  
  <pre><code>awk -F … somehow put the regex here with a search and replace "-" into "/"
  and save it as a variable.
  </code></pre>
  
  <p><strong>(4b)</strong> Create new target directory if it doesn't exist and move files there.</p>
  
  <pre><code>targetDir=$targetPath$datePath
  mkdir -p $targetDir
  mv -v "$i" "$destination"
  </code></pre>
  
  <p>PS: Bash would be nice.</p>