如果我有几个目录,我想要cp并重命名它们。
#For example.
> 1st_folder
>> Case_A
>>> Folder_A_Date_Case
>>> Folder_B_Date_Case_work
>>> Folder_C_Date_Case_work_Result
>> Case_B
>>> Folder_A_Date_Case
>>> Folder_B_Date_Case_work
>>> Folder_C_Date_Case_work_Result
> 2nd_folder
>> Case_A
>>> Folder_A_Date_Case
>>> Folder_B_Date_Case_work
>>> Folder_C_Date_Case_work_Result
>> Case_B
>>> Folder_A_Date_Case
>>> Folder_B_Date_Case_work
>>> Folder_C_Date_Case_work_Result
我想复制Folder_A
Folder_B
和Folder_C
,并将其重命名为Folder_D
Folder_E
和Folder_F
,如下所示。
> 1st_folder
>> Case_A
>>> Folder_A_Date_Case
>>> Folder_B_Date_Case_work
>>> Folder_C_Date_Case_work_Result
>>> Folder_D_Date_Case # copy from Folder_A
>>> Folder_E_Date_Case_work # copy from Folder_B
>>> Folder_F_Date_Case_work_Result # copy from Folder_C
>> Case_B
>>> Folder_A_Date_Case
>>> Folder_B_Date_Case_work
>>> Folder_C_Date_Case_work_Result
>>> Folder_D_Date_Case # copy from Folder_A
>>> Folder_E_Date_Case_work # copy from Folder_B
>>> Folder_F_Date_Case_work_Result # copy from Folder_C
....
..
.do the same thing as 2nd_folder
如何使用shell脚本快速完成?
答案 0 :(得分:1)
只需循环遍历文件夹,使用参数扩展创建新文件夹名称,然后使用cp -r
复制包含其所有内容的文件夹。
#!/bin/bash
for dir in *_folder ; do
for case in "$dir"/Case_*/ ; do
for folder in "$case"/Folder*/ ; do
newname=${folder/Folder_A/Folder_D}
newname=${newname/Folder_B/Folder_E}
newname=${newname/Folder_C/Folder_F}
cp -r "$folder" "${newname}"
done
done
done
如果Case的内容更具动态性,即可能有超过3个子文件夹,或者名称不遵循相同的模式,则您需要更具体。此外,在这种情况下,我可能会找到更具表现力的语言(例如Perl)。