注意:

#cat / tmp / foo - 常规文件

/lib/a.lib
/lib/b.lib
/lib/c.lib
/lib/d.lib

cat / tmp / foo | xargs cp / tmp / fred

cp:target /lib/d.lib不是目录

有帮助吗?

解决方案

xargs通常将其替换的args放在最后。你可以这么做:

$ cp `cat /tmp/foo` /tmp/fred/.

如果它只是lib文件,那么 cp /lib/?.lib / tmp / fred /.自然会有效。

要真正用 xargs 来做,这里是一个把arg放在第一位的例子:

0:~$ (echo word1; echo word2) | xargs -I here echo here how now
word1 how now
word2 how now
0:~$ 

其他提示

您的xargs版本可能接受 -I

xargs -I FOO cp FOO /tmp/fred/ < /tmp/foo

假设 / tmp / fred 是一个目录,请使用 -t - target-directory 选项)指定它:

$ cat /tmp/foo | xargs cp -t /tmp/fred

为什么不尝试这样的事情:

cp /lib/*.lib /tmp/fred

我认为你的命令失败了,因为xargs创建了以下命令:

cp /tmp/fred /lib/a.lib /lib/b.lib /lib/c.lib /lib/d.lib

也就是说,它最终会尝试将所有内容复制到/lib/d.lib,这不是目录,因此是您的错误消息。

目标目录需要是命令行中的最后一个,但是 xargs 会将stdin附加到命令行的末尾,因此在您尝试时它最终会成为第一个参数。

您可以在使用xargs之前将目标附加到/ tmp / foo,或者在反引号中使用cat在目标之前插入sorce文件:

    cp `cat /tmp/foo` /tmp/fred/

假设/ tmp / fred是一个存在的目录,你可以使用while循环

while read file
do
    cp $file /tmp/fred
done < /tmp/foo

正如Sinan&#220; n&#252; r

所回答
cat /tmp/foo | xargs cp -t /tmp/fred

如果没有-t支持

( cat /tmp/foo; echo /tmp/fred ) | xargs cp
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top