简体   繁体   English

如何在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

I have some files located in one directory /home/john I want to copy all the files with *.text extension from this directory and save them as *.text.bkup, again in the same directory, ie /home/john 我有一些文件位于一个目录/ home / john我想从这个目录中复制所有带* .text扩展名的文件并将它们保存为* .text.bkup,再次在同一目录中,即/ home / john

Is there a single command with which I can do that? 有一个命令可以用来做到这一点吗? Also, with extension of the same idea, is it possible to copy all the files with multiple extentions (eg *.text & *.doc) as *.text.bkup & *.doc.bkup repectively (again in the same directory)? 此外,通过扩展相同的想法,是否可以将具有多个扩展的所有文件(例如* .text&* .doc)分别复制为* .text.bkup&* .doc.bkup(再次在同一目录中) ?

This is best accomplished with a Shell loop: 这最好用Shell循环完成:

~/tmp$ touch one.text two.text three.doc four.doc  
~/tmp$ for FILE in *.text *.doc; do cp ${FILE} ${FILE}.bkup; done 
~/tmp$ ls -1
four.doc
four.doc.bkup
one.text
one.text.bkup
three.doc
three.doc.bkup
two.text
two.text.bkup

What happens in the code above is the shell gets all .text and .doc files and then loops through each value one by one, assigning the variable FILE to each value. 在上面的代码中发生的是shell获取所有.text和.doc文件,然后逐个循环遍历每个值,将变量FILE分配给每个值。 The code block between the "do" and the "done" is executed for every value of FILE, effectively copying each file to filename.bkup . 对于FILE的每个值,执行“do”和“done”之间的代码块,有效地将每个文件复制到filename.bkup

您可以通过查找轻松实现此目的:

find /home/john -iname '*.text' -type f -exec cp \{} \{}.backup \;

No, there is no single/simple command to achieve this with standard tools 不,没有单一/简单的命令来实现标准工具

But you can write a script like this to do it for you. 但你可以写一个这样的脚本来为你做。

for file in *.text
do
    cp -i "${file}" "${file}.bkup"
done

with -i option you can confirm each overwriting operation 使用-i选项,您可以确认每个覆盖操作

I sort of use a roundabout way to achieve this. 我有点使用迂回的方式来达到这个目的。 It involves a Perl script and needs additional steps. 它涉及Perl脚本,需要额外的步骤。

Step 1: Copy the names of all the text files into a text file. 步骤1: 将所有文本文件的名称复制到文本文件中。

find -maxdepth 1 -type f -name '*.txt' > file_name1.txt

Step 2: Make a duplicate of the copied file. 第2步: 复制复制的文件。

cp file_name1.txt file_name2.txt

Now open the file_name2.txt in vi editor and do a simple string substitution. 现在在vi编辑器中打开file_name2.txt并执行简单的字符串替换。

%s/.text/.text.backup/g

Step 3: Merge the source and destination file names into a single file separated by a comma. 步骤3: 将源文件名和目标文件名合并为一个用逗号分隔的文件。

paste -d, file_name1.txt file_name2.txt > file_name.txt

Step 4: Run the below perl script to achieve the desired results 第4步:运行以下perl脚本以获得所需的结果

open(FILE1,"<file_name.txt") or die'file doesnt exist'; #opens a file that has source and destination separated beforhand using commas
chomp(@F1_CONTENTS=(<FILE1>)); # copies the content of the file into an array
close FILE1;
while()
{
    foreach $f1 (@F1_CONTENTS) 
    {
    @file_name=split(/,/,$f1);  # separates the file content based on commas
        print "cp $file_name[0] $file_name[1]\n";
    system ("cp $file_name[0] $file_name[1]"); # performs the actual copy here
    }
last;
}

暂无
暂无

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

相关问题 Rsync:如何在循环中按顺序将文件从源复制到目标?(linux) - Rsync : How to copy files from source to destination in loop sequentially ?(linux) 如何将文件名更改为数字的文件复制到同一目录中 - How do I copy files in the same directory with a number changed of filename 如何在Linux中复制多个文件的开头? - How do I copy the beginning of multiple files in Linux? 如何在Linux的目录中合并多个文件,以使每个文件数据都放在新列中? - How do I combine multiple files in a directory in linux such that each files data is placed in a new column? 如何在bash中使用相同的名称但扩展名相同的目录复制多个文件? - How can I copy multiple files in the same directory with different names but same extension in bash? 我如何从另一个目录访问目录中的文件(linux) - how do i access files in directory from another directory (linux) 批量复制并重命名同一目录下的多个文件 - Batch copy and rename multiple files in the same directory 如何复制文件并授予它们目标目录的权限 - How to copy files and give them permission of destination directory Linux-通过覆盖将文件从源移动到目标 - Linux - moving files from source to destination with overwrite 在linux中,将多个目录中的同名文件复制到一个以路径不同为名称的新目录中 - In linux, copy files with the same name in multiple directories, to a new directory with the path difference as name
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM