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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
10-13-2012, 10:14 AM
|
#1
|
|
Senior Member
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,629
|
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!
|
|
|
|
10-13-2012, 10:38 AM
|
#2
|
|
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: 
|
Simplest solution is to do it with a loop.
The cp command doesn't support multiple destinations.
|
|
|
|
10-13-2012, 10:48 AM
|
#3
|
|
Member
Registered: Dec 2011
Location: Greece
Distribution: Slackware
Posts: 239
Rep:
|
Code:
cat A.txt | tee B.txt C.txt D.txt >/dev/null
|
|
|
1 members found this post helpful.
|
10-13-2012, 10:57 AM
|
#4
|
|
Senior Member
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,629
Original Poster
|
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.
|
|
|
|
10-13-2012, 11:01 AM
|
#5
|
|
Senior Member
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,629
Original Poster
|
Quote:
Originally Posted by kabamaru
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.
|
|
|
|
10-13-2012, 11:17 AM
|
#6
|
|
Senior Member
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 1,659
|
Quote:
Originally Posted by meninvenus
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.
|
|
|
|
10-13-2012, 11:21 AM
|
#7
|
|
LQ Newbie
Registered: Jun 2008
Distribution: Ubuntu, Debian, Mint, Fedora, Arch
Posts: 23
Rep:
|
or another way:
Code:
for i in {B,C,D}; do cp A.txt "$i".txt; done
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 02:36 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|