LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices



Reply
 
Search this Thread
Old 09-09-2004, 01:04 AM   #1
nikold01
LQ Newbie
 
Registered: Sep 2004
Posts: 4

Rep: Reputation: 0
How do I redirect something to a file without it overwriting the destination file???


The title says it all.

I am trying to write a script which takes the input from one file and processes it and then redirects the output to another file

My problem is that when i just use the > command it overwrites the destination file.

How do I fix this problem

Any help would be greatly appreciated
 
Old 09-09-2004, 01:37 AM   #2
Libu
Member
 
Registered: Oct 2003
Location: Chennai
Distribution: Slackware 12.1
Posts: 165

Rep: Reputation: 36
You can user >> instead of >. e.g. ls >> myfile
What this will do is append the new info to "myfile"
 
Old 09-09-2004, 01:41 AM   #3
dalek
Senior Member
 
Registered: Jul 2003
Location: Mississippi USA
Distribution: Gentoo
Posts: 2,058
Blog Entries: 2

Rep: Reputation: 65
Test this theory BEFORE you do it. I was told the other day the putting a > overwrites the file. However putting >> will add to the bottom of the file and leave the rest in tact. I think it is the bottom, it may be the top. I had a birthday recently, so I am getting older. Don't bug me about it.

Like I said, test this before you screw up something.

Later

 
Old 09-09-2004, 01:54 AM   #4
nitin_batta
Member
 
Registered: Aug 2004
Location: India
Distribution: Redhat Enterprise Server 2.1
Posts: 96

Rep: Reputation: 15
Quote:
You can user >> instead of >. e.g. ls >> myfile
What this will do is append the new info to "myfile"
One note using >> will not create a new file if it is not present.

if you plan to put the output in a new file do
Code:
$touch myfile
$ls >> myfile
 
Old 09-09-2004, 04:14 AM   #5
Ciccio
Member
 
Registered: Nov 2002
Location: Paraguay
Distribution: Mandrake 10
Posts: 573

Rep: Reputation: 30
Quote:
Originally posted by nitin_batta
One note using >> will not create a new file if it is not present.

if you plan to put the output in a new file do
Code:
$touch myfile
$ls >> myfile
I just tried using >> without the touch on my mandrake 10 and it actually creates the file... so... so I see no need of using touch for this. Although It can't hurt.

Again, asDalek says, you should test it before using it.

BTW:

Code:
cat <file> <whatever you want to add> > file
You can concatenate (cat) the file with whatever you want to add and the redirect it to the file itself... that should work.
Although I'm a and this could not work... I believe it would.

Last edited by Ciccio; 09-09-2004 at 04:16 AM.
 
Old 09-09-2004, 07:15 AM   #6
nitin_batta
Member
 
Registered: Aug 2004
Location: India
Distribution: Redhat Enterprise Server 2.1
Posts: 96

Rep: Reputation: 15
Quote:
I just tried using >> without the touch on my mandrake 10 and it actually creates the file... so... so I see no need of using touch for this. Although It can't hurt.
Every distro of Linux has some custom implementations or something like their signature on it
 
Old 09-09-2004, 08:00 AM   #7
nikold01
LQ Newbie
 
Registered: Sep 2004
Posts: 4

Original Poster
Rep: Reputation: 0
Hey, thanks everyone for your input, Much appreciated.
 
Old 09-09-2004, 10:12 AM   #8
stickman
Senior Member
 
Registered: Sep 2002
Location: Nashville, TN
Posts: 1,552

Rep: Reputation: 53
Quote:
Originally posted by nitin_batta
One note using >> will not create a new file if it is not present.

if you plan to put the output in a new file do
Code:
$touch myfile
$ls >> myfile
Using >> will create a file if the file does not exist and the user process has appropriate privelages. Appending to an existing file also requires appropriate privelages.
 
Old 09-09-2004, 03:30 PM   #9
Ciccio
Member
 
Registered: Nov 2002
Location: Paraguay
Distribution: Mandrake 10
Posts: 573

Rep: Reputation: 30
we are not talking about different implementations, we are talking about bash built-ins... it's different.
 
  


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
trying to redirect text to a file to cat at later point. says file doesn't exist. dr_zayus69 Programming 1 10-02-2005 09:10 AM
Using > to redirect to file DaHammer Programming 4 08-07-2005 01:05 AM
how do i redirect stderr to a file? nodger Programming 5 11-28-2004 12:22 PM
redirect into C++ without a file ? C++Boar Programming 1 04-24-2004 11:20 AM
symlinks are corrupted even thought the destination file is on its place jogurt666 Linux - General 5 12-19-2003 02:20 PM


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

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration