title: How to incorporate conditions in a for loop to create folder names based on file dates
body: <p>I've managed to create a script that loops through each file in a folder, extracts the year, month, and day, then creates a folder structure for that.</p>
  
  <p>For a group of files, I end up with folders like this:</p>
  
  <pre><code>2019
    02 (February)
      01 (Day 1)
    03
      01
      02
  </code></pre>
  
  <p><strong>Question</strong>:  How do I incorporate a comparison to already existing folders and stop the script from performing the same action over and over again?  I.E., MKDIR 2019, MKDIR 2019, etc.</p>
  
  <p>If there are 10 files that were created in 2019, then the script creates the folder on the first loop, and then the next 9 iterations reports an error as the folder already exists, despite it still giving the desired result.</p>
  
  <p>The same happens in the month folders and the day folders.</p>
  
  <p>Here is what I currently have:</p>
  
  <pre><code>#!/bin/bash
  
  for file in *
  
  do
  
  YEAR=$(date -r ${file} +"%Y")
  MONTH=$(date -r ${file} +"%m")
  DAYOFMONTH=$(date -r ${file} +"%d")
  
          for year in $YEAR
          do
  
                  mkdir $YEAR
  
                  for month in $MONTH
                  do
                  mkdir $YEAR/$MONTH 
                  done
  
                          for day in $DAYOFMONTH
                          do
                          mkdir $YEAR/$MONTH/$DAYOFMONTH
                          done
  
          done
  
  cp ${file} $YEAR/$MONTH/$DAYOFMONTH
  
  done
  </code></pre>