带有bash大括号扩展的cp copy命令

Dr *_*ard 6 bash bash-completion

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

cp file.txt test1.txt

但是如果我尝试将file.txt复制到这样的几个文件中

cp file.txt test{2..4}.txt
Run Code Online (Sandbox Code Playgroud)

我收到错误

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

Mic*_*ker 12

这不是关于bash,而是关于cp.如果为cp提供两个以上的参数,则最后一个参数应该是要复制所有其他参数的目录.

for f in test{2..4}.txt ; do cp file.txt $f ; done
Run Code Online (Sandbox Code Playgroud)


pep*_*uan 5

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

在DOS/Windows世界中,通配符由程序处理.因此,xcopy *.txt *.bak例如,手段xcopy有2个参数:*.txt*.bak.如何完全解释通配符取决于xcopy.

在*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

对于cp命令,这正是迈克尔所写的:如果你给出cp超过2个args,那么最后一个arg必须是一个目录.