[英]How can I copy multiple files in the same directory with different names but same extension in bash?
The requirement is to make copies of n(n>10000) files in the same linux directory. 要求是在同一linux目录中复制n(n> 10000)个文件。 The extension of the files has to be intact and can probably add numbers to distinguish among files. 文件的扩展名必须完整无缺,并且可能会添加数字以区分文件。
For eg if one file is text1.txt the other could be text2.txt 例如,如果一个文件是text1.txt,另一个文件可能是text2.txt
But I have to create multiple copies from multiple files and not from a single file. 但是我必须从多个文件而不是从单个文件创建多个副本。
Please help. 请帮忙。
Bash pattern substitution might help you here. 重击模式替换可能会在这里为您提供帮助。 If you eg want to copy all .txt files, you can do it like this: 例如,如果您想复制所有.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.