In python, you can
sys.stdout = open('log', 'w') # begin redirect
then output will write to log
instead.
You can restore back to normal behavior with
sys.stdout = sys.__stdout__ # end redirect, restore back
How to achieve similar effect in zsh & bash?
P.S.,
- stdout of command in subshell should also be redirected.
ls > log
is not what I want.
To clarify, what I want is
ls # output to terminal
# begin redirect to `log`
ls # output to `log`
find -type f # output to `log`
... # output to `log`
# end redirect, restore back
ls # output to terminal
Edit Below are not what I want
- redirection a group of command.
- tail -f for monitoring.
As The first few lines of this question stated, what I want is
# ...
cmd1 # normal behavior
# begin redirection
cmd2 # redirect to file
# some times later
cmd2 # redirect to file
# ...
cmdN # redirect to file
# end redirection
cmdN+1 # normal behavior
# ...