LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 06-04-2008, 12:40 PM   #1
tekmann33
Member
 
Registered: Nov 2006
Posts: 188

Rep: Reputation: 30
Write to multiple files...one command


Here is the first type of job I have had to do where I am making additions to multiple files, and I was wondering what the best way to go about it is.

I have to write the same lines to multiple file, too many to do it manually.

The directory structure is this (directory names have been changed to protect the innocent):

/usr/local/abc/abc_1/file
/user/local/abc/abc_2/file
/usr/local/abc/abc_3/file
...
...
..

I want to make the same entry into "file" for all of files under "abc/abc_*/".

What would be the best and safest command to accomplish this?
 
Old 06-04-2008, 12:56 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
You can try a for loop using the find command to list them, and edit the files one at a time. Or if you have simply to append some lines you can use tee, like this
Code:
echo line to append | tee /usr/local/abc/abc_?/file > /dev/null
the redirection to /dev/null is to prevent the terminal output. I wonder what are the original names of the directories...!
 
Old 06-04-2008, 01:00 PM   #3
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
Quote:
Originally Posted by colucix View Post
You can try a for loop using the find command to list them, and edit the files one at a time. Or if you have simply to append some lines you can use tee, like this
Code:
echo line to append | tee /usr/local/abc/abc_?/file > /dev/null
the redirection to /dev/null is to prevent the terminal output. I wonder what are the original names of the directories...!
Would that not overwrite the files? Would
Code:
echo line to append | tee -a /usr/local/abc/abc_?/file > /dev/null
be more appropriate?
 
Old 06-04-2008, 01:02 PM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Yes, I had it in mind. Just forgotten. Thanks, pwc101
 
  


Reply


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
how do I write a script using sed to delete the first and last line in multiple files Rikki D Programming 4 05-02-2008 11:01 PM
How to write a command to FTP files with dates filenames mistiqshannen Linux - Newbie 5 06-05-2007 12:59 AM
Shell scripting: How to write to multiple files? Micro420 Programming 14 05-19-2007 03:41 AM
Listing all write access files on command line akamad Linux - General 6 03-14-2007 11:37 AM
How to write multiple c++ files? Xiangbuilder Programming 2 09-15-2003 09:12 AM


All times are GMT -5. The time now is 05:56 PM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration