Question

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

Was it helpful?

Solution

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:~$ 

OTHER TIPS

Your version of xargs probably accepts -I:

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

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

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

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

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top