1838571 Members
3281 Online
110128 Solutions
New Discussion

Re: Outputs to a file

 
JOHN TURNER_2
Frequent Advisor

Outputs to a file

i have created a script to run a datacopy overnight and would like to output whether each command has been succesful into a file, im having trouble getting it outputting to the file without overwriting the previous output. There will be six commands and would like the outpt of all six commands in a file one after another


GUI's are for wimps!
10 REPLIES 10
Patrick Wallek
Honored Contributor

Re: Outputs to a file

The syntax you are probably using is:

somecommand > logfile

Which will overwrite every time.

What you should use is:

somecommand >> logfile

The >> is an append, so the next time you do a

someothercommand >> logfile

the output will be appended to the logfile.
John Poff
Honored Contributor

Re: Outputs to a file

Hi,

Have you tried using the '>>' operator for your redirection? The '>>' operator will append to a file while '>' will overwrite. You can do this:

ls >myfile
bdf >>myfile
date >>myfile


The first command overwrites the file and the following commands append to it.

JP
Hai Nguyen_1
Honored Contributor

Re: Outputs to a file

John,

You should use this symbol ">>" (without quotes) to append output to the same file which will keep the later output from overwriting the earlier output.

Hai
Jim Mallett
Honored Contributor

Re: Outputs to a file

Make sure you use >> rather than >

>> will append to, or create a file
> will create or overwrite a file

If thats not the case, include your script for our review.
Hindsight is 20/20
Dario_1
Trusted Contributor

Re: Outputs to a file

Hi:

Use >> to write to a log file.

command >> command.log

This will append the output to the log file.

If you use only > you will overwrite the file.

Regards,

DR
Bill McNAMARA_1
Honored Contributor

Re: Outputs to a file

you can also | through tee

ie command | tee /tmp/file.out

this way you can keep screen output.

Later,
Bill
It works for me (tm)
Bill McNAMARA_1
Honored Contributor

Re: Outputs to a file

you can also | through tee

ie command | tee /tmp/file.out

this way you can keep screen output.

Later,
Bill
It works for me (tm)
Paul Sperry
Honored Contributor

Re: Outputs to a file

Its easy to append output to the end of an existing file. For example,
$ cat afile >> bfile

appends the contents of the file afile to the end of the file bfile and
$ cmd >> bfile

Rodney Hills
Honored Contributor

Re: Outputs to a file

If you are running a script, do a -

exec >/tmp/mylog 2>&1

at the top of the script to redirect all output (STDOUT and STDERR) to mylog. Then all output from all commands will be redirected.

HTH

-- Rod Hills
There be dragons...
twang
Honored Contributor

Re: Outputs to a file

one more thing:
I prefer separating output and error:

# command 1>> out.log 2>>err.log
eg.
# date 1>>log 2>>err