tags:

views:

241

answers:

6

Note:

# cat /tmp/foo - regular file

/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

A: 

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.

dave
+6  A: 

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:~$
DigitalRoss
@DigitalRoss: Thank you Sir
Aaron
A: 

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/
moonshadow
+1  A: 

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

$ cat /tmp/foo | xargs cp -t /tmp/fred
Sinan Ünür
+1  A: 

Your version of xargs probably accepts -I:

xargs -I FOO cp FOO /tmp/fred/ < /tmp/foo
ndim
A: 

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
David Harris