bash 带有bash括号扩展的cp复制命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5580835/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 23:45:37  来源:igfitidea点击:

cp copy command with bash brace expansion

bashbash-completion

提问by Dr Blowhard

at the bash prompt I can perform this copy

在 bash 提示符下,我可以执行此副本

cp file.txt test1.txt

cp file.txt test1.txt

but if I try to copy file.txt to several files like so

但是如果我尝试将 file.txt 复制到多个文件中

cp file.txt test{2..4}.txt

I get error

我得到错误

cp: target `test4.txt' is not a directory

cp: 目标`test4.txt' 不是目录

回答by Michael Krelin - hacker

It's not about bash, it's about cp. If you supply cp with more than two parameters the last one should be a directory to which all others are to be copied.

这不是关于 bash,而是关于 cp。如果您为 cp 提供两个以上的参数,最后一个应该是一个目录,所有其他参数都将复制到该目录中。

for f in test{2..4}.txt ; do cp file.txt $f ; done

回答by pepoluan

Well, you have to understand how *nix shells work.

好吧,您必须了解 *nix shell 是如何工作的。

In the DOS/Windows world, wildcards are handled by the programs. Thus, xcopy *.txt *.bak, for instance, means xcopyis given 2 parameters: *.txtand *.bak. How the wildcards are interpreted fully depends on xcopy.

在 DOS/Windows 世界中,通配符由程序处理。因此,xcopy *.txt *.bak例如,meansxcopy有两个参数:*.txt*.bak。通配符的解释方式完全取决于xcopy.

In the *nix world, wildcards are handled by the shell. A similar command xcopy *.txt *.bak, for instance, gets expanded first becoming xcopy <list of files ending with .txt> <list of files ending with .back>. Thus assuming the existence of file1.txtto file4.txt, plus another file old.bak, the command will be expanded to xcopy file1.txt file2.txt file3.txt file4.txt old.bak

在 *nix 世界中,通配符由 shell 处理。xcopy *.txt *.bak例如,一个类似的命令首先扩展为xcopy <list of files ending with .txt> <list of files ending with .back>. 因此假设存在file1.txtto file4.txt,加上另一个文件old.bak,命令将扩展为xcopy file1.txt file2.txt file3.txt file4.txt old.bak

For the cpcommand, it's exactly what Michael has written: If you give cpmore than 2 args, the last arg must be a directory.

对于cp命令,这正是 Michael 所写的:如果您提供cp2 个以上的 args,则最后一个 arg 必须是目录。