Forum Moderators: bakedjake

Message Too Old, No Replies

Copying 1 to Many (using 'cp'?)

Can I make many copies of one file?

         

Nick_W

8:17 am on Sep 5, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi all,

If I have a file named 'one' and would like to 'cp' it to 'two', 'three' and 'four', how might that be done?

Doesn't seem possible just using the cp flags?

Many thanks...

Nick

Duckula

8:35 am on Sep 5, 2003 (gmt 0)

10+ Year Member



I don't know if the bare 'cp' can do this, but using bash

for i in bar baz quux; do cp foo $i; done

does the trick and can be easily turned into an alias or script.

Nick_W

8:39 am on Sep 5, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks Duckula, I'm a little lost though ;)

Could you fit that in with my example so that I know which bit is which?

Much thanks...

Nick

Duckula

8:58 am on Sep 5, 2003 (gmt 0)

10+ Year Member



"foo" is the original file; it's a 'for' loop, it does

cp foo bar
cp foo baz
cp foo quux

in that order. To make an script from it, take the arguments list, split it between the source and the destiny list and substitute (pseudocode - does not include extracting arguments):

for i in $DESTLIST; do cp $SOURCE $i; done

In your example it would be:

for i in two three four; do cp one $i; done

Nick_W

9:06 am on Sep 5, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Fantastic. Very much appreciated Duckla.

Thankyou ;)

Nick