请帮我重命名多个文件。用filemask生成日常三报表的一个应用
OPEN_REPORTn_yyyymmddHH24Miss.csv
,例如这样的列表:
/mnt/server/OPEN_REPORT1_20180604130922.csv
/mnt/server/OPEN_REPORT2_20180604130922.csv
/mnt/server/OPEN_REPORT3_20180604130922.csv
我希望这些文件复制为
/mnt/server/OPEN_REPORT1.csv
/mnt/server/OPEN_REPORT2.csv
/mnt/server/OPEN_REPORT3.csv
保留原始文件而不更改名称(因此,这意味着我只能列出最后3个文件)
我有这个解决方案:
cp $(ls -t /mnt/server/OPEN_REPORT1_* | head -n1) /mnt/server/OPEN_REPORT1.csv
cp $(ls -t /mnt/server/OPEN_REPORT2_* | head -n1) /mnt/server/OPEN_REPORT2.csv
cp $(ls -t /mnt/server/OPEN_REPORT3_* | head -n1) /mnt/server/OPEN_REPORT3.csv
但是这个方法不是很有效,因为我用了更多的
cp
我需要的命令。我只想拷贝这些文件
内容提供商
命令和正则表达式。
我正在尝试这样的解决方案:
for file in $(ls -t /mnt/server/OPEN_REPORT?_??????????????.csv | head -n3); do echo ${file} | sed 's/OPEN_REPORT([0-9]{1})/$1/'; done
但回声的结果看起来不太好。
有什么解决办法吗?谢谢你的建议
解决方案
(感谢
David Peltier
):
for file in $(ls -t /mnt/server/OPEN_REPORT?_??????????????.csv | head -n3); do cp $file ${file%_*}.csv; done