简体   繁体   English

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

[英]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.

相关问题 如何将变量分配给 bash 中同一目录中文件的不同扩展名 - How to assign variable to different extension of files in same directory in bash 在Linux中,如何查找相同目录下具有相同扩展名的文件并将其复制到一个目录中? - How does one find and copy files of the same extension, in different directories, to a single directory in linux? 重命名与目录同名的文件-bash脚本 - Renaming files with the same names as directory - bash script Linux Bash:将多个不同的文件移动到同一目录 - Linux Bash: Move multiple different files into same directory 如何在linux中一次复制多个文件? 这些文件的源位置和目标位置是同一目录 - How do I copy multiple files at once in linux? With the source and the destination locations of these files being the same directory 如何在使用bash更改名称和扩展名的同时将多个文件移动到目录中? - How can I move multiple files to a directory while changing their names and extensions using bash? 批量复制并重命名同一目录下的多个文件 - Batch copy and rename multiple files in the same directory Linux - 如何将位于多个子目录中的相同扩展名的文件直接复制到一个子目录中? - Linux - How can I copy files of the same extension located in several subdirectories into a single directly? 如何将文件名更改为数字的文件复制到同一目录中 - How do I copy files in the same directory with a number changed of filename 仅在下面的目录中查找和同名文件,然后复制到其他目录 - Find and files of same name only in directory below and copy to a different directory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM