Ender28
Ender28

Reputation: 43

xargs to copy one file into several

I have a directory that has one file with information (call it masterfile.inc) and several files that are empty (call them file1.inc-file20.inc)

I'm trying to formulate an xargs command that copies the contents of masterfile.inc into all of the empty files.

So far I have

ls -ltr | awk '{print $9}' | grep -v masterfile | xargs -I {} cat masterfile.inc > {}

Unfortunately, all this does is creates a file called {} and prints masterfile.inc into it N times.

Is there something I'm missing with the syntax here?

Thanks in advance

Upvotes: 1

Views: 660

Answers (2)

Ole Tange
Ole Tange

Reputation: 33725

While the tee trick is really brilliant you might be interested in a solution that is easier to adapt for other situations. Here using GNU Parallel:

ls -ltr | awk '{print $9}' | grep -v masterfile | parallel "cat masterfile.inc > {}"

It takes literally 10 seconds to install GNU Parallel:

wget pi.dk/3 -qO - | sh -x

Watch the intro videos to learn more: https://www.youtube.com/playlist?list=PL284C9FF2488BC6D1

Upvotes: 0

kev
kev

Reputation: 161864

You can use this command to copy file 20 times:

$ tee <masterfile.inc >/dev/null file{1..20}.inc

Note: file{1..20}.inc will expand to file1, file2, ... , file20


If you disternation filenames are random:

$ shopt -s extglob
$ tee <masterfile.inc >/dev/null $(ls !(masterfile.inc))

Note: $(ls !(masterfile.inc)) will expand to all file in current directory except masterfile.inc (please don't use spaces in filename)

Upvotes: 1

Related Questions