Bash – Copying a file onto several other files with different names

bashlinuxshell-script

I have one file (lets call it file1.xyz) that I want to use as a template to work on. I need to copy the contents of file1.xyz so that they replace the contents of the other files – file2.xyz, file3.xyz, file4.xyz, file5.xyz…..file70.xyz whilst keeping the original file name.

I have tried using:

cp file1.xyz *.xyz

The files are all in the same directory and I don't want to append them to each other.

This has not worked, how can I solve this problem?

Best Answer

With zsh:

f=(file*.xyz)
cat $f[1] > $f[2,-1]

That writes all output files in parallel though (as if using tee) which means that doesn't scale well to large number of files.

With any Bourne-like shell (including zsh and bash), you could always do:

set file*.xzy
source=$1; shift
for dest do cp "$source" "$dest"; done
Related Question