title: Shell script to find file and copy file from other location
body: <p>I'm puzzeling with a shell script.</p>
  
  <p>What am I trying to achieve?</p>
  
  <ul>
  <li>In my 'upload2website'-folder on my Mac I have numerous files that have the following <em>AA_bestandsnaam-xx.jpg</em> or <em>AA_bestandsnaam - xx.jpg</em> (Watch the spaces). xx is a filenumber, could be any number.</li>
  <li>Below the 'upload2website'-folder there a several subfolders.</li>
  <li>In the subfolder below upload2website there are the origins of <em>AA_bestandnaam-xx.jpg</em> or <em>AA_bestandnaam - xx.jpg</em></li>
  <li>If the file is in <em>upload2website</em> is found in one of the subfolders, then copy it to the folders <em>uploads</em></li>
  <li>It is possible that the AA is placed at the end of the file, f.i. <em>bestandnaam-xx_AA.jpg</em></li>
  </ul>
  
  <p><strong>Example of the structure</strong></p>
  
  <p>upload2website-folder</p>
  
  <ul>
  <li>AA_bestandsnaamA-68.jpg </li>
  <li>AA_bestandsnaamB - 01.jpg</li>
  <li>AA_bestandsnaamC-106.jpg </li>
  <li>AA_bestandsnaamD-30.jpg</li>
  </ul>
  
  <p>subfolders</p>
  
  <ul>
  <li>upload2website/folder1/bestandsnaamA-13.jpg</li>
  <li>upload2website/folder1/bestandsnaamA-28.jpg</li>
  <li>upload2website/folder1/bestandsnaamA-68.jpg</li>
  <li>upload2website/folder1/bestandsnaamA-90.jpg</li>
  <li>upload2website/folder2/bestandsnaamB - 08.jpg</li>
  <li>upload2website/folder2/bestandsnaamB - 09.jpg</li>
  <li>upload2website/folder2/bestandsnaamB - 10.jpg</li>
  <li>upload2website/folder2/bestandsnaamB - 13.jpg</li>
  <li>upload2website/folder2/bestandsnaamB - 01.jpg</li>
  <li>upload2website/folder3/bestandsnaamC-08.jpg</li>
  <li>upload2website/folder3/bestandsnaamC-90.jpg</li>
  <li>upload2website/folder3/bestandsnaamC-105.jpg</li>
  <li>upload2website/folder3/bestandsnaamC-106.jpg</li>
  <li>etc.</li>
  </ul>
  
  <p>The following script I'm working on, but that doesn't work, f.i. if there are spaces in the name, than name is truncated as of the first space. I already added -iname and quotes around the variables.</p>
  
  <pre><code>#!/bin/sh
  SRCDIR=upload2website
  DSTDIR=uploads
  PREFIX=FB_
  UPLOAD=`ls "$SRCDIR"/*.jpg`
  #SEARCH=`$UPLOAD | sed "/$PREFIX/s///"`
  for src in $UPLOAD
  do
     # echo source: "$src"
     test="`basename "$src"`"
     FBname=`basename "$src"  | sed "/$PREFIX/s///"`
     # name=`basename "$src"`
     # echo name: "$name"
  
     #dst=`find "$DSTDIR" -name "$FBname"`
     dst=`find "$SRCDIR" -iname "$FBname"`
     # if [ "$dst" != "" ]; then
  
     if [ "$dst" != "" ]; then
      echo FBname: "$FBname"
       echo cp "$dst" "$DSTDIR"
     fi
  done
  </code></pre>
  
  <p>Can somebody please help me to get the script right?</p>
  
  <p><strong>Update: Clarify folders</strong></p>
  
  <p>the folderstructure:</p>
  
  <ul>
  <li>/Users/gjdegraaf/Documents/upload2website  &lt;&lt;&lt;--- contains subfolders and origin of the image files</li>
  <li>/Users/gjdegraaf/Documents/uploads. &lt;&lt;&lt;--- should contain the copied origin of the file that matches the file without AA_</li>
  </ul>