Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

So I echo output of my programs ran in terminal by command 2>&1 >> /path/toFile.txt

It creates few limitations that I want to overcome:

  1. It "freezes" and gives no output in terminal - only way to see actual output is to open output file
  2. Opening output file is not giving live results. It gives results from time of opening that file

How to echo output to a file and be able to:

a) see live changes in that file (separate program would be needed I assume)

or

b) output to file and see that output in terminal in the same time

share|improve this question
    
There is a problem with the command - more details please. –  suspectus Dec 28 '13 at 9:15

2 Answers 2

up vote 8 down vote accepted

For a) There is a utility called "tail" that shows the last few lines of a file and optionally monitors a file for new lines added to the end:

$ tail -f /path/toFile.txt

The less pager also has the ability to follow changes, pressing shift-F causes it to enter a mode much like tail -f, with the advantage that a single press of ctrl-c will get you back into the normal file viewing mode, so that you can scroll backwards through the file or search through it without closing it.

For b) There is a standard utility called tee for just this purpose (http://www.gnu.org/software/coreutils/manual/coreutils.html#tee-invocation)

You'll probably want something like the following:

command 2>&1 | tee -a /path/toFile.txt | less

The | less at the end is obviously optional, but be aware that if you include it, closing down less will probably also close down the command you are running.

As an aside, if this is to be a long running process, strongly consider either running it under nohup or running it inside screen, so that it survives a logout.

share|improve this answer
    
What is it 'The less pager' –  Ultra Dec 29 '13 at 12:53
    
less is a "pager" (a program that lets you scroll through files on the terminal), a drop in replacement for the traditional "more" pager with the ability to go backwards as well as forwards. (en.wikipedia.org/wiki/Less_%28Unix%29) –  Stephanie Dec 29 '13 at 15:06

This sounds like a job for tee. The output will simultaneously be written to the terminal and to the file.

command 2>&1 | tee -a /path/toFile.txt
share|improve this answer
1  
Since the command in the question appends to a file, it's good to know that this command will overwrite the file, but you can specify -a to append instead. –  brm Dec 29 '13 at 15:12
1  
@brm - Good point, I didn't notice the >> in the original question. Answer has been edited to include -a –  dbenham Dec 29 '13 at 15:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.