简体   繁体   English

批量复制并重命名同一目录下的多个文件

[英]Batch copy and rename multiple files in the same directory

I have 20 files like:我有 20 个文件,例如:

01a_AAA_qwe.sh
01b_AAA_asd.sh
01c_AAA_zxc.sh
01d_AAA_rty.sh
...

Files have a similar format in their names.文件的名称具有相似的格式。 They begin with 01, and they have 01* AAA *.sh format.它们以 01 开头,具有 01* AAA *.sh 格式。

I wish to copy and rename files in the same directory, changing the number 01 to 02, 03, 04, and 05:我希望复制并重命名同一目录中的文件,将数字 01 更改为 02、03、04 和 05:

02a_AAA_qwe.sh
02b_AAA_asd.sh
02c_AAA_zxc.sh
02d_AAA_rty.sh
...

03a_AAA_qwe.sh
03b_AAA_asd.sh
03c_AAA_zxc.sh
03d_AAA_rty.sh
...

04a_AAA_qwe.sh
04b_AAA_asd.sh
04c_AAA_zxc.sh
04d_AAA_rty.sh
...

05a_AAA_qwe.sh
05b_AAA_asd.sh
05c_AAA_zxc.sh
05d_AAA_rty.sh
...

I wish to copy 20 of 01*.sh files to 02*.sh, 03*.sh, and 04*.sh.我希望将 01*.sh 文件中的 20 个复制到 02*.sh、03*.sh 和 04*.sh。 This will make the total number of files to 100 in the folder.这将使文件夹中的文件总数达到 100。

I'm really not sure how can I achieve this.我真的不知道我怎么能做到这一点。 I was trying to use for loop in the bash script.我试图在 bash 脚本中使用 for 循环。 But not even sure what should I need to select as a for loop index.但甚至不确定我应该选择什么作为 for 循环索引。

for i in {1..4}; do
   cp 0${i}*.sh 0${i+1}*.sh
done

does not work.不起作用。

You can't do multiple copies in a single cp command, except when copying a bunch of files to a single target directory.您不能在单个cp命令中进行多次复制,除非将一堆文件复制到单个目标目录。 cp will not do the name mapping automatically. cp不会自动进行名称映射。 Wildcards are expanded by the shell, they're not seen by the commands themselves, so it's not possible for them to do pattern matching like this.通配符由 shell 扩展,命令本身看不到它们,因此它们不可能像这样进行模式匹配。

To add 1 to a variable, use $((i+1)) .要将 1 添加到变量,请使用$((i+1))

You can use the shell substring expansion operator to get the part of the filename after the first two characters.您可以使用 shell 子字符串扩展运算符来获取文件名前两个字符之后的部分。

for i in {1..4}; do
    for file in 0${i}*.sh; do
        fileend=${file:2}
        cp "$file" "0$((i+1))$fileend"
    done
done

There are going to be a lot of ways to slice-n-dice this one ...将有很多方法可以将这个切块...

One idea using a for loop, printf + brace expansion, and xargs :使用for循环、 printf + 大括号扩展和xargs一个想法:

for f in 01*.sh
do
    printf "%s\n" {02..05} | xargs -r -I PFX cp ${f} PFX${f:2}
done

Another idea using a pair of for loops:使用一对for循环的另一个想法:

for f in 01*.sh
do
    for i in {02..05}
    do
        cp "${f}" "${i}${f:2}"
    done
done

Starting with:从...开始:

$ ls -1 0*.sh
01a_AAA_qwe.sh
01b_AAA_asd.sh
01c_AAA_zxc.sh
01d_AAA_rty.sh

Both of the proposed code snippets leave us with:两个提议的代码片段都给我们留下了:

$ ls -1 0*.sh
01a_AAA_qwe.sh
01b_AAA_asd.sh
01c_AAA_zxc.sh
01d_AAA_rty.sh

02a_AAA_qwe.sh
02b_AAA_asd.sh
02c_AAA_zxc.sh
02d_AAA_rty.sh

03a_AAA_qwe.sh
03b_AAA_asd.sh
03c_AAA_zxc.sh
03d_AAA_rty.sh

04a_AAA_qwe.sh
04b_AAA_asd.sh
04c_AAA_zxc.sh
04d_AAA_rty.sh

05a_AAA_qwe.sh
05b_AAA_asd.sh
05c_AAA_zxc.sh
05d_AAA_rty.sh

NOTE: blank lines added for readability注意:为了可读性添加了空行

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 重命名Linux目录中的多个文件 - Rename Multiple Files in a Directory in Linux 查找所有扩展名为.sh .cpp .c的文件,并将其复制到我的桌面目录中,如果存在同名文件,则将其重命名 - find all files with .sh .cpp .c extension and copy them to a directory in my desktop, if there is a file with the same name, rename it linux如何将不同文件夹下的同名文件批量复制重命名到单独的文件夹,文件重命名为父文件夹? - How do batch copy and rename files with same names under different folders to a separate folder with files renamed to their parent folder in linux? 动态重命名HDFS目录中的多个文件 - Rename multiple files in HDFS directory dynamically 将多个目录中的文件重命名为目录名称 - Rename files in multiple directories to the name of the directory 脚本或Java程序来重命名目录中的多个文件 - script or a java program to rename multiple files in a directory 批量重命名文件以在Linux中包含文件夹/目录名称 - Batch rename files to include folder/directory name in linux 关于linux cp同一目录与其他目录相似的文件并重命名? - Regarding linux cp same directory Similar files to other directory and rename? 如何在bash中使用相同的名称但扩展名相同的目录复制多个文件? - How can I copy multiple files in the same directory with different names but same extension in bash? 批量重命名文件 - Batch rename files
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM