如何在Linux中使用一个命令将文件复制到多个目录

00_lead_image_copying_to_three_locations_with_one_command

Copying a file with the Linux command line is easy. However, what if you want to copy the same file to several different locations? That’s easy, too, and we’ll show you how to do that with one command.

使用Linux命令行复制文件很容易。 但是,如果要将同一文件复制到几个不同的位置怎么办? 这也很容易,我们将向您展示如何使用一个命令来执行此操作。

Normally, to copy one file, you would use the cp command, linking to the source file and the destination directory:

通常,要复制一个文件,可以使用cp命令,链接到源文件和目标目录:

cp ~/Documents/FileToBeCopied.txt ~/TextFiles/

To copy it to two more directories, many people would just run the command two more times, with different destinations:

要将其复制到另外两个目录中,许多人将在不同的目的地再运行该命令两次:

cp ~/Documents/FileToBeCopied.txt ~/Dropbox/
cp ~/Documents/FileToBeCopied.txt /media/lori/MYUSBDRIVE/
01_entering_cp_command_three_times

However, we can do the same task with one command:

但是,我们可以使用一个命令执行相同的任务:

echo dir1 dir2 dir3 | xargs -n 1 cp file1

Here’s how this command works. The echo command normally writes to the screen. However, in this case, we want to feed the output of the echo command as input to the xargs command. To do this, we use the pipe symbol ( | ) which feeds output from one command as input to another. The xargs command will run the cp command three times, each time appending the next directory path piped to it from the echo command on to the end of the cp command. There are three arguments being passed to xargs , but the -n 1 option on the xargs command tells it to only append one of those arguments at a time to the cp command each time it’s run.

这是此命令的工作方式。 echo命令通常会写入屏幕。 但是,在这种情况下,我们希望将echo命令的输出作为xargs命令的输入。 为此,我们使用管道符号( | )将一个命令的输出作为另一命令的输入。 xargs命令将运行cp命令3次,每次将从echo命令到cp命令末尾的下一个目录路径附加到管道中。 有三个参数传递给xargs ,但是xargs命令上的-n 1选项告诉它每次运行时仅一次将这些参数之一附加到cp命令。

So, to stick with our example from earlier, the three separate cp commands above can be combined into one command like this:

因此,以前面的示例为例,可以将上述三个单独的cp命令组合为一个命令,如下所示:

echo ~/TextFiles/ ~/Dropbox /media/lori/MYUSBDRIVE | xargs -n 1 cp ~/Documents/FileToBeCopied.txt
02_using_single_command

Note that if the file being copied exists in any of the destination directories specified, the file in that destination will be replaced automatically. You will not be asked if you want to replace the file. (Normally, when you use the cp command to copy a file to a single location, you can add the -i option to ask if you want to replace an existing file. However, the -i option is an interactive option (it causes the cp command to ask for input from the user) and you cannot use an interactive option with the cp command when using it in conjunction with xargs .)

请注意,如果要复制的文件存在于指定的任何目标目录中,则该目标中的文件将被自动替换。 不会询问您是否要替换文件。 (通常,当您使用cp命令将文件复制到单个位置时,可以添加-i选项以询问是否要替换现有文件。但是, -i选项是一个交互式选项(它会导致cp命令来请求用户输入),并且在与xargs结合使用cp命令时,不能将其与交互式选项一起使用。)

One other thing to consider, is that if you are copying a very large file, you might want to add the no-clobber ( -n ) option to the cp command in the single command above. This option automatically prevents a file from being overwritten in a destination if it already exists there. If you’re copying a very large file over a network, it may be slow and you might want to avoid using the resources required to copy and replace the file. The following command adds the -n option, and will not copy the file to any destination listed in the arguments to the echo statement, if the file already exists in that destination.

要考虑的另一件事是,如果要复制很大的文件,则可能要在上述单个命令的cp命令中添加no-clobber( -n )选项。 如果目标中已经存在文件,此选项会自动防止文件被覆盖。 如果要通过网络复制非常大的文件,可能会很慢,并且可能要避免使用复制和替换文件所需的资源。 以下命令添加了-n选项,并且不会将文件复制到echo语句的参数中列出的任何目标位置(如果该目标文件中已经存在该文件)。

echo ~/TextFiles/ ~/Dropbox /media/lori/MYUSBDRIVE | xargs -n 1 cp -n ~/Documents/FileToBeCopied.txt
03_single_command_with_no_clobber

Type man echo, man xargs, or man cp on the command line in Linux for more information about any of these commands.

在Linux的命令行中键入man echoman xargsman cp ,以获取有关这些命令中任何命令的更多信息。

翻译自: https://www.howtogeek.com/284829/how-to-copy-a-file-to-multiple-directories-with-one-command-in-linux/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值