Echoing output to file while seeing it in console in the same time [duplicate]

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


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.


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