简体   繁体   English

如何使用shell复制和重命名多个文件

[英]How to Copy and Rename multiple files using shell

I want to copy only 20180721 files from Outgoing to Incoming folder.我只想将20180721文件从Outgoing复制到Incoming文件夹。 I also want to remove the first numbers from the file name and want to rename from -1 to -3 .我还想从文件名中删除第一个数字,并想从 -1 重命名为 -3 I want to keep my commands to minimum so I am using pax command below.我想保持我的命令最少,所以我在下面使用pax命令。

Filename:文件名:

216118105741_MOM-09330-20180721_102408-1.jar

Output expected:预期输出:

MOM-09330-20180721_102408-3.jar

I have tried this command and it's doing most of the work apart from removing the number coming in front of the file name.我已经尝试过这个命令,除了删除文件名前面的数字外,它正在完成大部分工作。 Can anyone help?任何人都可以帮忙吗?

Command used:使用的命令:

pax -rw -pe -s/-1/-3/ ./*20180721*.jar ../Incoming/

You can try this 你可以试试这个

In general 一般来说

for i in `ls files-to-copy-*`; do 
   cp $i `echo $i | sed "s/rename-from/rename-to/g"`; 
done;

In your case 就你而言

for i in `ls *_MOM*`; do 
   cp $i `echo $i | sed "s/_MOM/MOM/g" | sed "s/-1/-3/g"`; 
done;

Try this simple script using just parameter expansion : 仅使用参数扩展尝试以下简单脚本:

for file in *20180721*.jar; do
    new=${file#*_}
    cp -- "$file" "/path/to/destination/${new%-*}-3.jar"
done

pax only applies the first successful substitution even if the -s option is specified more than once.即使多次指定-s选项, pax只会应用第一次成功的替换。 You can pipe the output to a second pax instance, though.不过,您可以将输出通过管道传输到第二个 pax 实例。

pax -w -s ':^[^_]*_::p' *20180721*.jar | (builtin cd ../Incoming; pax -r -s ':1[.]jar$:3.jar:p')

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM