title: How do I pass dirname to filename for multiple files in multiple directories?
body: <p>I have multiple test directories with very specific names, and within those directories I have multiple sub directories with multiple sub directories.  </p>
  
  <p>Example: </p>
  
  <p>*./V01A1A01/S01/R00/JADE/Anytest.drec<br>
  *./V01A1A01/S01/R01/JADE/Anytest.drec (note sub dir R01 is different)</p>
  
  <p>etc...</p>
  
  <p>*./V01A1A01/S02/R00/JADE/Anytest.drec<br>
  *./V01A1A01/S02/R01/JADE/Anytest.drec (note S02 and sub dir R01 are now different)</p>
  
  <p>etc...</p>
  
  <p>I want to replace and rename the <code>Anytest.drec</code> in all directories with a source file "Anytext.drec" (path=/home/mike/Desktop/Active_Test/Source_Files/Anytest.drec) that I change from time to time. I want the final name in the /JADE directory to take on the dirname so that Anytest.drec now reads = V01_A1_A01_S01_R00.drec.</p>
  
  <p>Here's my code so far:</p>
  
  <pre><code>#!/bin/bash 
  
  path1=/home/mike/Desktop/Active_Test/Source_Files/Anytest.drec  #set path of source Anytest.drec file
  set -x  #allows me to see what things are going on with code
  echo "Please enter the Master folder you wish to search or update"   #asks user for the input folder to search
  read "folder"       #creates variable "$folder" and sets the directory I want to search
  jade_files=`find /home/mike/Desktop/Active_Test/$folder -name "*.drec" -print`
  
  #finds all filenames in var $folder with the extension of .drec and puts the result in var $jade_files which now = /home/mike/Desktop/Active_Test/V01A1A02/S00/R00/JADE/Anytest.drec
  path=`dirname "$jade_files"`  #dirname gives full paths of .drec files and puts result into var $path 
  ext=${jade_files##*.}       #uses the var $jade_file to get the extension of .drec files found in the find search which = drec
  basename=`basename "$jade_files"`  #gets the basenames from var $file which = Anytest.drec
  
  #here's where my problems start... I'm trying to use the content of var "$jade_folders" to cp /Source_Files/Anytest.drec into var "$jade_folders" path(s) and using awk to parse out the portion of the dirname want to reconfigure into the filename
  while read -r line; do   
      new_name=| `awk '{print $5, "_", $6, "_", $7 }'`
      echo $new_name
      cp -b /home/mike/Desktop/Active_Test/Source_Files/"$new_name"."$ext" "$line"
  done &lt;&lt;&lt; "$jade_folders"  #essentially, this var is the input to the while loop and &lt;&lt;&lt; is how it's done.
  </code></pre>
  
  <p>What I get out is this:</p>
  
  <pre><code>cp: cannot stat ‘/home/mike/Desktop/Active_Test/Source_Files/.drec’: No such file or directory
  </code></pre>
  
  <p>How can I fix this script to make it do what I want?</p>
  
  <p>okay, this is the fix i ended up using, it doesn't put all the underscores but it puts in most.  I'll fix the rest as I learn more.  Thanks Ed:</p>
  
  <pre><code>    #!/bin/bash 
      path1=/home/mike/Desktop/Active_Test/Source_Files/Anytest.drec
      set -x
      echo "Please enter the Master folder you wish to search or update"
      read "folder"
      jade_files=`find /home/mike/Desktop/Active_Test/$folder -name "*.drec" -print`
      path=`dirname "$jade_files"`
      ext=${jade_files##*.}
      basename=`basename "$jade_files"` 
      cp -b "$path1" "$jade_files"."$ext"
  </code></pre>