In bash, calling foo
would display any output from that command on the stdout.
在bash中,调用foo将在stdout上显示该命令的任何输出。
Calling foo > output
would redirect any output from that command to the file specified (in this case 'output').
调用foo >输出会将该命令的任何输出重定向到指定的文件(在本例中为“output”)。
Is there a way to redirect output to a file and have it display on stdout?
是否有办法将输出重定向到一个文件并将其显示在stdout上?
9 个解决方案
#1
850
The command you want is named tee
:
您需要的命令名为tee:
foo | tee output.file
For example, if you only care about stdout:
例如,如果你只关心stdout:
ls -a | tee output.file
If you want to include stderr, do:
如果您想包含stderr,请这样做:
program [arguments...] 2>&1 | tee outfile
2>&1
redirects channel 2 (stderr/standard error) into channel 1 (stdout/standard output), such that both is written as stdout. It is also directed to the given output file as of the tee
command.
>&1将通道2 (stderr/标准错误)重定向到通道1 (stdout/标准输出),这样两者都被写入stdout。它还被指向tee命令的给定输出文件。
Furthermore, if you want to append to the log file, use tee -a
as:
此外,如果要附加到日志文件,请使用tee -a作为:
program [arguments...] 2>&1 | tee -a outfile
#2
433
$ program [arguments...] 2>&1 | tee outfile
2>&1
dumps the stderr and stdout streams. tee outfile
takes the stream it gets and writes it to the screen and to the file "outfile".
>和1转储stderr和stdout流。tee outfile将它获取的流写入到屏幕和文件“outfile”中。
This is probably what most people are looking for. The likely situation is some program or script is working hard for a long time and producing a lot of output. The user wants to check it periodically for progress, but also wants the output written to a file.
这可能是大多数人正在寻找的。可能的情况是,一些程序或脚本在长时间内都在努力工作并产生大量的输出。用户希望定期检查它的进度,但也希望将输出写到文件中。
The problem (especially when mixing stdout and stderr streams) is that there is reliance on the streams being flushed by the program. If, for example, all the writes to stdout are not flushed, but all the writes to stderr are flushed, then they'll end up out of chronological order in the output file and on the screen.
问题(特别是在混合stdout和stderr流时)是依赖于被程序刷新的流。例如,如果不刷新对stdout的所有写操作,而是刷新对stderr的所有写操作,那么它们将在输出文件和屏幕上按照时间顺序结束。
It's also bad if the program only outputs 1 or 2 lines every few minutes to report progress. In such a case, if the output was not flushed by the program, the user wouldn't even see any output on the screen for hours, because none of it would get pushed through the pipe for hours.
如果程序每隔几分钟只输出1或2行来报告进度,那也很糟糕。在这种情况下,如果程序没有刷新输出,用户甚至在数小时内都不会看到屏幕上的任何输出,因为这些输出都不会在管道中被推送数小时。
Update: The program unbuffer
, part of the expect
package, will solve the buffering problem. This will cause stdout and stderr to write to the screen and file immediately and keep them in sync when being combined and redirected to tee
. E.g.:
更新:程序unbuffer是expect包的一部分,它将解决缓冲问题。这将导致stdout和stderr立即写入屏幕和文件,并在合并和重定向到tee时保持它们的同步。例如:
$ unbuffer program [arguments...] 2>&1 | tee outfile
#3
87
Another way that works for me is,
另一种对我有效的方法是,
<command> |& tee <outputFile>
as shown in gnu bash manual
如gnu bash手册所示
Example:
例子:
ls |& tee files.txt
If ‘|&’ is used, command1’s standard error, in addition to its standard output, is connected to command2’s standard input through the pipe; it is shorthand for 2>&1 |. This implicit redirection of the standard error to the standard output is performed after any redirections specified by the command.
如果使用' |& ',command1的标准错误除其标准输出外,还通过管道连接到command2的标准输入;它是2>和1 |的简写。标准输出的这个隐式重定向是在命令指定的任何重定向之后执行的。
For more information, refer redirection
有关更多信息,请参考重定向
#4
#5
8
using tail -f output
should work.
使用尾部-f输出应该可以工作。
#6
8
Something to add ...
添加的东西……
The package unbuffer has support issues with some packages under fedora and redhat unix releases.
包unbuffer在fedora和redhat unix版本下支持一些包的问题。
Setting aside the troubles
留出的麻烦
Following worked for me
以下为我工作
bash myscript.sh 2>&1 | tee output.log
Thank you ScDF & matthew your inputs saved me lot of time..
感谢ScDF和matthew你的投入节省了我很多时间。
#7
3
Bonus answer since this use-case brought me here:
这个用例给了我额外的回答:
In the case where you need to do this as some other user
如果你需要像其他用户那样做的话
echo "some output" | sudo -u some_user tee /some/path/some_file
Note that the echo will happen as you and the file write will happen as "some_user" what will NOT work is if you were to run the echo as "some_user" and redirect the output with >> "some_file" because the file redirect will happen as you.
请注意,当您和文件写将以“some_user”的形式发生时,将会发生echo,但如果您以“some_user”的形式运行echo并使用>>“some_file”重定向输出,则不会发生这种情况,因为文件重定向将在您运行时发生。
Hint: tee also supports append with the -a flag, if you need to replace a line in a file as another user you could execute sed as the desired user.
提示:tee还支持带有-a标志的追加,如果需要将文件中的一行替换为另一个用户,则可以将sed作为所需的用户执行。
#8
0
< command > |& tee filename
# this will create a file "filename" with command status as a content, If a file already exists it will remove existed content and writes the command status.
<命令> |& tee文件名#这将创建一个文件“filename”,命令状态作为内容,如果一个文件已经存在,它将删除现有的内容并写入命令状态。
< command > | tee >> filename
# this will append status to the file but it doesn't print the command status on standard_output (screen).
< command > | tee >> filename #这将向文件添加状态,但它不会在standard_output(屏幕)上打印命令状态。
I want to print something by using "echo" on screen and append that echoed data to a file
我想通过在屏幕上使用“echo”并将该回显数据附加到文件中来打印一些东西
echo "hi there, Have to print this on screen and append to a file"
#9
-10
tee is perfect for this, but this will also do the job
tee是完美的,但它也会做这个工作。
ls -lr / > output | cat output
#1
850
The command you want is named tee
:
您需要的命令名为tee:
foo | tee output.file
For example, if you only care about stdout:
例如,如果你只关心stdout:
ls -a | tee output.file
If you want to include stderr, do:
如果您想包含stderr,请这样做:
program [arguments...] 2>&1 | tee outfile
2>&1
redirects channel 2 (stderr/standard error) into channel 1 (stdout/standard output), such that both is written as stdout. It is also directed to the given output file as of the tee
command.
>&1将通道2 (stderr/标准错误)重定向到通道1 (stdout/标准输出),这样两者都被写入stdout。它还被指向tee命令的给定输出文件。
Furthermore, if you want to append to the log file, use tee -a
as:
此外,如果要附加到日志文件,请使用tee -a作为:
program [arguments...] 2>&1 | tee -a outfile
#2
433
$ program [arguments...] 2>&1 | tee outfile
2>&1
dumps the stderr and stdout streams. tee outfile
takes the stream it gets and writes it to the screen and to the file "outfile".
>和1转储stderr和stdout流。tee outfile将它获取的流写入到屏幕和文件“outfile”中。
This is probably what most people are looking for. The likely situation is some program or script is working hard for a long time and producing a lot of output. The user wants to check it periodically for progress, but also wants the output written to a file.
这可能是大多数人正在寻找的。可能的情况是,一些程序或脚本在长时间内都在努力工作并产生大量的输出。用户希望定期检查它的进度,但也希望将输出写到文件中。
The problem (especially when mixing stdout and stderr streams) is that there is reliance on the streams being flushed by the program. If, for example, all the writes to stdout are not flushed, but all the writes to stderr are flushed, then they'll end up out of chronological order in the output file and on the screen.
问题(特别是在混合stdout和stderr流时)是依赖于被程序刷新的流。例如,如果不刷新对stdout的所有写操作,而是刷新对stderr的所有写操作,那么它们将在输出文件和屏幕上按照时间顺序结束。
It's also bad if the program only outputs 1 or 2 lines every few minutes to report progress. In such a case, if the output was not flushed by the program, the user wouldn't even see any output on the screen for hours, because none of it would get pushed through the pipe for hours.
如果程序每隔几分钟只输出1或2行来报告进度,那也很糟糕。在这种情况下,如果程序没有刷新输出,用户甚至在数小时内都不会看到屏幕上的任何输出,因为这些输出都不会在管道中被推送数小时。
Update: The program unbuffer
, part of the expect
package, will solve the buffering problem. This will cause stdout and stderr to write to the screen and file immediately and keep them in sync when being combined and redirected to tee
. E.g.:
更新:程序unbuffer是expect包的一部分,它将解决缓冲问题。这将导致stdout和stderr立即写入屏幕和文件,并在合并和重定向到tee时保持它们的同步。例如:
$ unbuffer program [arguments...] 2>&1 | tee outfile
#3
87
Another way that works for me is,
另一种对我有效的方法是,
<command> |& tee <outputFile>
as shown in gnu bash manual
如gnu bash手册所示
Example:
例子:
ls |& tee files.txt
If ‘|&’ is used, command1’s standard error, in addition to its standard output, is connected to command2’s standard input through the pipe; it is shorthand for 2>&1 |. This implicit redirection of the standard error to the standard output is performed after any redirections specified by the command.
如果使用' |& ',command1的标准错误除其标准输出外,还通过管道连接到command2的标准输入;它是2>和1 |的简写。标准输出的这个隐式重定向是在命令指定的任何重定向之后执行的。
For more information, refer redirection
有关更多信息,请参考重定向
#4
16
You can primarily use Zoredache solution, but If you don't want to overwrite the output file you should write tee with -a option as follow :
您可以主要使用Zoredache解决方案,但是如果您不想覆盖输出文件,您应该使用-a选项编写tee,如下所示:
ls -lR / | tee -a output.file
#5
8
using tail -f output
should work.
使用尾部-f输出应该可以工作。
#6
8
Something to add ...
添加的东西……
The package unbuffer has support issues with some packages under fedora and redhat unix releases.
包unbuffer在fedora和redhat unix版本下支持一些包的问题。
Setting aside the troubles
留出的麻烦
Following worked for me
以下为我工作
bash myscript.sh 2>&1 | tee output.log
Thank you ScDF & matthew your inputs saved me lot of time..
感谢ScDF和matthew你的投入节省了我很多时间。
#7
3
Bonus answer since this use-case brought me here:
这个用例给了我额外的回答:
In the case where you need to do this as some other user
如果你需要像其他用户那样做的话
echo "some output" | sudo -u some_user tee /some/path/some_file
Note that the echo will happen as you and the file write will happen as "some_user" what will NOT work is if you were to run the echo as "some_user" and redirect the output with >> "some_file" because the file redirect will happen as you.
请注意,当您和文件写将以“some_user”的形式发生时,将会发生echo,但如果您以“some_user”的形式运行echo并使用>>“some_file”重定向输出,则不会发生这种情况,因为文件重定向将在您运行时发生。
Hint: tee also supports append with the -a flag, if you need to replace a line in a file as another user you could execute sed as the desired user.
提示:tee还支持带有-a标志的追加,如果需要将文件中的一行替换为另一个用户,则可以将sed作为所需的用户执行。
#8
0
< command > |& tee filename
# this will create a file "filename" with command status as a content, If a file already exists it will remove existed content and writes the command status.
<命令> |& tee文件名#这将创建一个文件“filename”,命令状态作为内容,如果一个文件已经存在,它将删除现有的内容并写入命令状态。
< command > | tee >> filename
# this will append status to the file but it doesn't print the command status on standard_output (screen).
< command > | tee >> filename #这将向文件添加状态,但它不会在standard_output(屏幕)上打印命令状态。
I want to print something by using "echo" on screen and append that echoed data to a file
我想通过在屏幕上使用“echo”并将该回显数据附加到文件中来打印一些东西
echo "hi there, Have to print this on screen and append to a file"
#9
-10
tee is perfect for this, but this will also do the job
tee是完美的,但它也会做这个工作。
ls -lr / > output | cat output