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?

  • Blindly copy it to all other "fellow" files inside a directory? – Jacob Vlijm Mar 6 '17 at 16:54

With zsh:

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

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
    Since the question has a Bash tag, you can also substitute `seq 2 70` with {2..70}. – David Foerster Mar 6 '17 at 22:43

The simplest way you can do by tee command

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.