Linux 命令输出重定向到文件和终端
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13591374/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Command output redirect to file and terminal
提问by Satish
I am trying to throw command output to file plus console also. This is because i want to keep record of output in file. I am doing following and it appending to file but not printing ls
output on terminal.
我也试图将命令输出抛出到文件和控制台。这是因为我想在文件中保留输出记录。我正在执行以下操作并将其附加到文件但不在ls
终端上打印输出。
$ls 2>&1 > /tmp/ls.txt
采纳答案by Karoly Horvath
Yes, if you redirect the output, it won't appear on the console. Use tee
.
是的,如果您重定向输出,它将不会出现在控制台上。使用tee
.
ls 2>&1 | tee /tmp/ls.txt
回答by Farah
In case somebody needs to append the output and not overriding, it is possible to use "-a" or "--append" option of "tee" command :
如果有人需要附加输出而不是覆盖,可以使用“tee”命令的“-a”或“--append”选项:
ls 2>&1 | tee -a /tmp/ls.txt
ls 2>&1 | tee --append /tmp/ls.txt
回答by Serge Rogatch
It is worth mentioning that 2>&1 means that standard error will be redirected too, together with standard output. So
值得一提的是, 2>&1 意味着标准错误也将与标准输出一起重定向。所以
someCommand | tee someFile
gives you just the standard output in the file, but not the standard error: standard error will appear in console only. To get standard error in the file too, you can use
只给你文件中的标准输出,而不是标准错误:标准错误只会出现在控制台中。要在文件中也获得标准错误,您可以使用
someCommand 2>&1 | tee someFile
(source: In the shell, what is " 2>&1 "?). Finally, both the above commands will truncate the file and start clear. If you use a sequence of commands, you may want to get output&error of all of them, one after another. In this case you can use -a flag to "tee" command:
(来源:在 shell 中,什么是“2>&1”?)。最后,上述两个命令都会截断文件并开始清除。如果您使用一系列命令,您可能希望一个接一个地获得所有这些命令的输出和错误。在这种情况下,您可以使用 -a 标志来“tee”命令:
someCommand 2>&1 | tee -a someFile