开发者

How to use cp from stdin?

开发者 https://www.devze.com 2022-12-09 09:39 出处:网络
Note: # cat /tmp/foo - regular fil开发者_高级运维e /lib/a.lib /lib/b.lib /lib/c.lib /lib/d.lib cat /tmp/foo | xargs cp /tmp/fred

Note:

# cat /tmp/foo - regular fil开发者_高级运维e

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

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

cp: target /lib/d.lib is not a directory


xargs normally places its substituted args last. You could just do:

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

If it's really just the lib files, then cp /lib/?.lib /tmp/fred/. would naturally work.

And to really do it with xargs, here is an example of putting the arg first:

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


Assuming /tmp/fred is a directory, specify it using the -t (--target-directory option):

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


Your version of xargs probably accepts -I:

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


As answered by Sinan Ünür

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

if no -t support

( cat /tmp/foo; echo /tmp/fred ) | xargs cp


Why not try something like:

cp /lib/*.lib /tmp/fred

I think your command is failing because xargs creates the following command:

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

That is, it ends up trying to copy everything to /lib/d.lib, which is not a directory, hence your error message.


The destination directory needs to be the last thing on the command line, however xargs appends stdin to the end of the command line so in your attempt it ends up as the first argument.

You could append the destination to /tmp/foo before using xargs, or use cat in backticks to interpolate the sorce files before the destination:

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


Assuming /tmp/fred is a directory that exists, you could use a while loop

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

精彩评论

暂无评论...
验证码 换一张
取 消