How to use cp from stdin?

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

+3
source share
7 answers

xargs usually puts its substituted arguments last. You could just do:

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

If these are really just lib files, then it cp /lib/?.lib /tmp/fred/.will work fine.

And in order to do this with xargs, here is an example of entering an argument:

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

Assuming it /tmp/fredis a directory, specify it using the -t( --target-directory) parameter :

$ cat /tmp/foo | xargs cp -t /tmp/fred
+2
source

xargs, , -I:

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

- :

cp /lib/*.lib /tmp/fred

, , xargs :

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

, /lib/d.lib, , .

0

, xargs stdin , .

/tmp/foo xargs cat backticks sorce :

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

/tmp/fred - , , while

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

Sinan Ünür

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

-t

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

Source: https://habr.com/ru/post/1720838/


All Articles