3

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?

1
  • Blindly copy it to all other "fellow" files inside a directory? Commented Mar 6, 2017 at 16:54

4 Answers 4

4

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
3

Here's a one-liner that would create the files in your example:

for i in {2..70}; do cp file1.xyz file$i.xyz; done
1
  • 1
    Since the question has a Bash tag, you can also substitute `seq 2 70` with {2..70}. Commented Mar 6, 2017 at 22:43
2

The simplest way you can do by tee command

cat file1.xyz | tee *.xyz >/dev/null
3
  • This does not create the files file2.xyz through file70.xyz
    – cherdt
    Commented Mar 6, 2017 at 17:17
  • Author is asking about to replace the content of file,so i thought the file is already there Commented Mar 6, 2017 at 17:19
  • I see, you're right -- the OP does imply that the other files are already there. Your solution accomplishes that.
    – cherdt
    Commented Mar 6, 2017 at 17:32
2
echo file{2..70}.xyz | xargs -n 1 -t cp file1.xyz
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .