如何将dirname传递给多个目录中多个文件的filename?

时间:2014-09-06 19:09:47

标签: bash variables awk dirname

我有多个具有非常特定名称的测试目录,并且在这些目录中我有多个具有多个子目录的子目录。

示例:

*。/ V01A1A01 / S01 / R00 / JADE / Anytest.drec
* ./ V01A1A01 / S01 / R01 / JADE / Anytest.drec(注意子目录R01不同)

等...

*。/ V01A1A01 / S02 / R00 / JADE / Anytest.drec
* ./ V01A1A01 / S02 / R01 / JADE / Anytest.drec(注意S02和子目录R01现在不同)

等...

我想用所有时间更改的源文件“Anytext.drec”(path = / home / mike / Desktop / Active_Test / Source_Files / Anytest.drec)替换并重命名所有目录中的Anytest.drec时间。我希望/ JADE目录中的最终名称采用dirname,以便Anytest.drec现在读取= V01_A1_A01_S01_R00.drec。

到目前为止,这是我的代码:

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

我得到的是:

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

如何修复此脚本以使其达到我想要的效果?

好吧,这是我最终使用的修复,它没有放下所有的下划线,但它放在最多。随着我了解更多,我会解决剩下的问题。谢谢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"

1 个答案:

答案 0 :(得分:1)

我无法弄清楚你是在做什么并尝试这样做让我们首先清理你脚本的语法,然后看看它是否符合你的要求,如果没有,你可以告诉它我们有什么不对。

试试这个:

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"

特别是new_name的设置只是猜测你正在尝试做什么。

以上是否符合您的要求?如果不是,需要做些什么?