title: How do I move files to specific directories based on a pattern in the filename?
body: <p>If any of this isn't particularly clear, please let me know and I'll do my best to clarify.</p>
  
  <p>I basically need to sort a set of files with various extensions and similar patterns to the filename, into directories and subdirectories that match the pattern and type of extension.</p>
  
  <p>To elaborate a bit:</p>
  
  <ol>
  <li><p>All files, regardless of extension, begin with the pattern "zz####" where #### is a number from 1 to 900; "zz1.zip through zz950.zip, zz1.mov through zz950.mov, zz1.mp4 through zz950.mp4"</p></li>
  <li><p>Some files contain additional characters; "zz360_hello_world.zip"</p></li>
  <li><p>Some files contain spaces; "zz370_hello world.zip"</p></li>
  <li><p>I need these files to be sorted and moved into directories and subdirectories following a particular format: "/home/hello/zz1/zip, /home/hello/zz1/vid"</p></li>
  <li><p>If the directories and/or subdirectories don't exist, I need them created.</p></li>
  </ol>
  
  <p>Example:</p>
  
  <pre><code>zz400_testing.zip       ----&gt; /home/hello/zz400/zip
  zz400 testing video.mov ----&gt; /home/hello/zz400/vid
  
  zz500.zip                       ----&gt; /home/hello/zz500/zip
  zz500_testing another video.mp4 ----&gt; /home/hello/zz500/vid
  </code></pre>
  
  <p>I found a few answers around here for simpler use-cases, but wasn't able to get anything working for my particular needs.</p>
  
  <p>Any help at all would be much appreciated.</p>
  
  <p>Thank you!</p>
  
  <p>EDIT: Adding the code I've been messing with</p>
  
  <pre><code>for f in *.zip; do
      set=`echo "$f"|sed 's/[0-9].*//'`
      dir="/home/demo/$set/photos"
      mkdir -p "$dir"
      mv "$f" "$dir"
  done
  </code></pre>
  
  <p>I think I'm just having trouble wrapping my head around how to match with regex. I've got this far with it:</p>
  
  <pre><code>[demo@alpha grep]$ echo zz433.zip|sed 's/[0-9].*//'
  zz
  </code></pre>
  
  <p>The script will run the mkdir, and even move the zip files into their proper place. I just can't get it to create the proper top-level directory (zz433).</p>