How to use cp from stdin?

Aaron picture Aaron · Oct 22, 2009 · Viewed 7k times · Source

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

Answer

DigitalRoss picture DigitalRoss · Oct 22, 2009

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