LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 10-13-2012, 10:14 AM   #1
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,797
Blog Entries: 4

Rep: Reputation: 285Reputation: 285Reputation: 285
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!
 
Old 10-13-2012, 10:38 AM   #2
JaseP
Senior Member
 
Registered: Jun 2002
Location: Eastern PA, USA
Distribution: K/Ubuntu 10.04/12.04, Scientific Linux 6.3, Android-x86, Maemo
Posts: 1,658

Rep: Reputation: 138Reputation: 138
Simplest solution is to do it with a loop.

The cp command doesn't support multiple destinations.
 
Old 10-13-2012, 10:48 AM   #3
kabamaru
Member
 
Registered: Dec 2011
Location: Greece
Distribution: Slackware
Posts: 276

Rep: Reputation: 133Reputation: 133
Code:
cat A.txt | tee B.txt C.txt D.txt >/dev/null
 
1 members found this post helpful.
Old 10-13-2012, 10:57 AM   #4
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,797
Blog Entries: 4

Original Poster
Rep: Reputation: 285Reputation: 285Reputation: 285
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.

Last edited by shivaa; 10-14-2012 at 08:34 AM.
 
Old 10-13-2012, 11:01 AM   #5
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,797
Blog Entries: 4

Original Poster
Rep: Reputation: 285Reputation: 285Reputation: 285
Quote:
Originally Posted by kabamaru View Post
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!

Last edited by shivaa; 10-13-2012 at 11:24 AM.
 
Old 10-13-2012, 11:17 AM   #6
suicidaleggroll
Senior Member
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 2,725

Rep: Reputation: 975Reputation: 975Reputation: 975Reputation: 975Reputation: 975Reputation: 975Reputation: 975Reputation: 975
Quote:
Originally Posted by meninvenus View Post
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.

Last edited by suicidaleggroll; 10-13-2012 at 11:19 AM.
 
Old 10-13-2012, 11:21 AM   #7
torchnw
LQ Newbie
 
Registered: Jun 2008
Distribution: Ubuntu, Debian, Mint, Fedora, Arch
Posts: 23

Rep: Reputation: 4
or another way:

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


Reply

Tags
copy


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Copy lines starting and ending with specific pattern from multiple files to a file ssn Linux - Newbie 2 07-27-2011 10:44 AM
[SOLVED] copy lines from multiple files to one file using sed -w cruzdelsur Programming 12 08-26-2010 08:24 AM
How to copy specific content of a dynamic file to another flie abhi1 Linux - Software 3 04-02-2009 06:15 AM
copy multiple files into one file HelpPlease Programming 2 12-09-2003 02:36 PM
How to copy a selected content from a .PDF file satimis Linux - General 3 07-03-2003 04:18 AM


All times are GMT -5. The time now is 03:22 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration