繁体   English   中英

如何在bash中使用相同的名称但扩展名相同的目录复制多个文件?

[英]How can I copy multiple files in the same directory with different names but same extension in bash?

要求是在同一linux目录中复制n(n> 10000)个文件。 文件的扩展名必须完整无缺,并且可能会添加数字以区分文件。

例如,如果一个文件是text1.txt,另一个文件可能是text2.txt

但是我必须从多个文件而不是从单个文件创建多个副本。

请帮忙。

重击模式替换可能会在这里为您提供帮助。 例如,如果您想复制所有.txt文件,则可以这样做:

for file in *.txt # add any other name wildcards
do
    filename=${file%.*} # removes everything after the last dot
    extension=${file##*.} # removes everything before the last dot
    cp "$file" "${filename}-copy.${extension}" # adds the -copy suffix to every copy
done

您可能希望研究logrotate之类的工具,该工具可以包含例如glob并定期旋转每个文件。

暂无
暂无

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

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