
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/

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

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

Type man echo, man xargs, or man cp on the command line in Linux for more information about any of these commands.
在Linux的命令行中键入man echo , man xargs或man cp ,以获取有关这些命令中任何命令的更多信息。