如何将多个目录中的多个文件的目录名传递到文件名



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

示例:

*/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"(路径=/home/mike/Desktop/Active_Test/source_Files/Anytest.drec)替换并重命名所有目录中的Anytest.drec。我希望/JADE目录中的最终名称采用目录名,以便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"

我不知道你想做什么,所以让我们先清理脚本的语法,然后看看它是否符合你的要求,如果没有,你可以告诉我们出了什么问题。

试试这个:

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的设置在很大程度上只是猜测,这就是你想要做的。

以上内容符合您的要求吗?如果没有,需要采取哪些不同的做法?

相关内容

  • 没有找到相关文章

最新更新