LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   How do I redirect something to a file without it overwriting the destination file??? (http://www.linuxquestions.org/questions/linux-general-1/how-do-i-redirect-something-to-a-file-without-it-overwriting-the-destination-file-228437/)

nikold01 09-09-2004 12:04 AM

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

Libu 09-09-2004 12:37 AM

You can user >> instead of >. e.g. ls >> myfile
What this will do is append the new info to "myfile"

dalek 09-09-2004 12:41 AM

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. :D

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

Later

:D :D :D :D

nitin_batta 09-09-2004 12:54 AM

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


Ciccio 09-09-2004 03:14 AM

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... :Pengy: 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 :newbie: and this could not work... I believe it would.

nitin_batta 09-09-2004 06:15 AM

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 :scratch:

nikold01 09-09-2004 07:00 AM

Hey, thanks everyone for your input, Much appreciated.
:)

stickman 09-09-2004 09:12 AM

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.

Ciccio 09-09-2004 02:30 PM

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


All times are GMT -5. The time now is 02:18 PM.