Mike Davis
Mike Davis

Reputation: 11

How do I pass dirname to filename for multiple files in multiple directories?

I have multiple test directories with very specific names, and within those directories I have multiple sub directories with multiple sub directories.

Example:

*./V01A1A01/S01/R00/JADE/Anytest.drec
*./V01A1A01/S01/R01/JADE/Anytest.drec (note sub dir R01 is different)

etc...

*./V01A1A01/S02/R00/JADE/Anytest.drec
*./V01A1A01/S02/R01/JADE/Anytest.drec (note S02 and sub dir R01 are now different)

etc...

I want to replace and rename the Anytest.drec 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.

Here's my code so far:

#!/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 <<< "$jade_folders"  #essentially, this var is the input to the while loop and <<< is how it's done.

What I get out is this:

cp: cannot stat ‘/home/mike/Desktop/Active_Test/Source_Files/.drec’: No such file or directory

How can I fix this script to make it do what I want?

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:

    #!/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"

Upvotes: 0

Views: 1425

Answers (1)

Ed Morton
Ed Morton

Reputation: 203229

I can't figure out what it is you're trying to do so let's first cleanup the syntax of your script and then see if it does what you want and if not you can tell us what's wrong.

Try this:

echo "Please enter the Master directory you wish to search or update"
read dir
jade_files=$(find /home/mike/Desktop/Active_Test/"$dir" -name '*.drec' -print)

while IFS= read -r line; do   
    path=$(dirname "$line")
    ext="${line##*.}"
    basename=$(basename "$line")  
    new_name=$(echo "$basename" | awk '{print $5, "_", $6, "_", $7 }')
    echo "$new_name"
    cp -b /home/mike/Desktop/Active_Test/Source_Files/"${new_name}.${ext}" "$line"
done <<< "$jade_files"

The setting of new_name in particular is very much just a guess that that's what you were trying to do.

Does the above do what you want? If not, what needs to be done differently?

Upvotes: 1

Related Questions