LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Copy content of a file into multiple files (https://www.linuxquestions.org/questions/linux-newbie-8/copy-content-of-a-file-into-multiple-files-4175432014/)

shivaa 10-13-2012 10:14 AM

Copy content of a file into multiple files
 
Hello everyone,
I am trying to copy the content of a text file into multiple files in a single command. Is there any way to achieve this?
For example, A.txt contains:
This is a simple text file.

And I want to copy this content into some other files, named B.txt, C.txt ... so on.

Thansk for your help!

JaseP 10-13-2012 10:38 AM

Simplest solution is to do it with a loop.

The cp command doesn't support multiple destinations.

kabamaru 10-13-2012 10:48 AM

Code:

cat A.txt | tee B.txt C.txt D.txt >/dev/null

shivaa 10-13-2012 10:57 AM

Alright, we can achive this using following simple script:

#!/bin/sh
for i in `cat fileslist.txt`
do
more [file] > $i
done


For users information, [file] is the file whose conetent you want to copy in all other files. And secondly, put a list of all files, in which you want to copy ccontent of the [file] file, into fileslist.txt file.

shivaa 10-13-2012 11:01 AM

Quote:

Originally Posted by kabamaru (Post 4804732)
Code:

cat A.txt | tee B.txt C.txt D.txt >/dev/null

I hadn't used tee command for a long time, so perhaps it skipped from my mind. Thanks, you make me remember that!

suicidaleggroll 10-13-2012 11:17 AM

Quote:

Originally Posted by meninvenus (Post 4804741)
Alright, we can achive this using following simple script:

#!/bin/sh
for i in `cat fileslist.txt`
do
more [file-to-copied] > $i
done


For users information, put a list of all files in which you want to copy ccontent of the [file-to-copied] file.

You shouldn't be running "for i in `cat fileslist.txt`", as the for loop will split on any spaces in those filenames. Using a "while read" would avoid this problem.

torchnw 10-13-2012 11:21 AM

or another way:

Code:

for i in {B,C,D}; do cp A.txt "$i".txt; done


All times are GMT -5. The time now is 11:50 AM.