How to redirect output to a file and stdout
In bash, calling foo would display any output from that command on the stdout.
Calling foo > output would redirect any output from that command to the file specified (in this case ‘output’).
Is there a way to redirect output to a file and have it display on stdout?
10 Answers 10
The command you want is named tee :
For example, if you only care about stdout:
If you want to include stderr, do:
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.
Furthermore, if you want to append to the log file, use tee -a as:
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».
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.
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.
Redirect all output to file [duplicate]
I know that in Linux, to redirect output from the screen to a file, I can either use the > or tee . However, I’m not sure why part of the output is still output to the screen and not written to the file.
Is there a way to redirect all output to file?
10 Answers 10
That part is written to stderr, use 2> to redirect it. For example:
or if you want in same file:
Note: this works in (ba)sh, check your shell for proper syntax
All POSIX operating systems have 3 streams: stdin, stdout, and stderr. stdin is the input, which can accept the stdout or stderr. stdout is the primary output, which is redirected with > , >> , or | . stderr is the error output, which is handled separately so that any exceptions do not get passed to a command or written to a file that it might break; normally, this is sent to a log of some kind, or dumped directly, even when the stdout is redirected. To redirect both to the same place, use:
EDIT: thanks to Zack for pointing out that the above solution is not portable—use instead:
If you want to silence the error, do:
To get the output on the console AND in a file file.txt for example.
Note: & (in 2>&1 ) specifies that 1 is not a file name but a file descriptor.
Redirect all output to file using Bash on Linux? [duplicate]
I am trying to redirect all output from a command line programme to a file. I am using Bash. Some of the output is directed to a the file, but some still appears in the terminal and is not stored to the file.
Similar symptoms are described here:
However I have tried the proposed solution (capture stderr) without success:
The file stderr.txt is created but is empty.
A possible clue is that the command-line programme is a client communicating with a server on the same machine. It may be that some of the output is coming from the server.
Is there a way to capture all the output from the terminal, irrespective of its origin?
EDIT:
I’ve confirmed that the missing output is generated by the server. Running the command in a separate terminal causes some output in both terminals, I can pipe all the output from the command terminal to a file. This raises issues about how to capture the server output, but that’s a different question.
How to redirect the output of the time command to a file in Linux?
Just a little question about timing programs on Linux: the time command allows to measure the execution time of a program:
Which works fine. But if I try to redirect the output to a file, it fails.
I know there are other implementations of time with the option -o to write a file but my question is about the command without those options.
11 Answers 11
which combines the STDERR of «time» and your command into time.txt
which puts STDERR from «sleep» into the file «sleep.stderr» and only STDERR from «time» goes into «time.txt»
Simple. The GNU time utility has an option for that.
But you have to ensure that you are not using your shell’s builtin time command, at least the bash builtin does not provide that option! That’s why you need to give the full path of the time utility:
Wrap time and the command you are timing in a set of brackets.
For example, the following times ls and writes the result of ls and the results of the timing into outfile :
Or, if you’d like to separate the output of the command from the captured output from time :
If you care about the command’s error output you can separate them like this while still using the built-in time command.
As you see the command’s errors go to a file (since stderr is used for time ).
Unfortunately you can’t send it to another handle (like 3>&2 ) since that will not exist anymore outside the
That said, if you can use GNU time, just do what @Tim Ludwinski said.
Command output redirect to file and terminal [duplicate]
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.
3 Answers 3
Yes, if you redirect the output, it won’t appear on the console. Use tee .
It is worth mentioning that 2>&1 means that standard error will be redirected too, together with standard output. So
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
(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:
How to redirect and append both stdout and stderr to a file with Bash?
To redirect stdout to a truncated file in Bash, I know to use:
To redirect stdout in Bash, appending to a file, I know to use:
To redirect both stdout and stderr to a truncated file, I know to use:
How do I redirect both stdout and stderr appending to a file? cmd &>> file.txt did not work for me.
7 Answers 7
Bash executes the redirects from left to right as follows:
- >>file.txt : Open file.txt in append mode and redirect stdout there.
- 2>&1 : Redirect stderr to «where stdout is currently going». In this case, that is a file opened in append mode. In other words, the &1 reuses the file descriptor which stdout currently uses.
There are two ways to do this, depending on your Bash version.
The classic and portable (Bash pre-4) way is:
A nonportable way, starting with Bash 4 is
(analog to &> outfile )
For good coding style, you should
- decide if portability is a concern (then use classic way)
- decide if portability even to Bash pre-4 is a concern (then use classic way)
- no matter which syntax you use, not change it within the same script (confusion!)
If your script already starts with #!/bin/sh (no matter if intended or not), then the Bash 4 solution, and in general any Bash-specific code, is not the way to go.
Also remember that Bash 4 &>> is just shorter syntax — it does not introduce any new functionality or anything like that.
In Bash you can also explicitly specify your redirects to different files:
Appending would be:
In Bash 4 (as well as ZSH 4.3.11):
This should work fine:
It will store all logs in file.txt as well as dump them on terminal.
Your usage of &>x.file does work in bash4. sorry for that : (
Here comes some additional tips.
0, 1, 2. 9 are file descriptors in bash.
0 stands for stdin , 1 stands for stdout , 2 stands for stderror . 3
9 is spare for any other temporary usage.
Any file descriptor can be redirected to other file descriptor or file by using operator > or >> (append).
Usage: >
I am surprised that in almost ten years, no one has posted this approach yet:
If using older versions of bash where &>> isn’t available, you also can do:
This spawns a subshell, so it’s less efficient than the traditional approach of cmd >> file.txt 2>&1 , and it consequently won’t work for commands that need to modify the current shell (e.g. cd , pushd ), but this approach feels more natural and understandable to me:
- Redirect stderr to stdout.
- Redirect the new stdout by appending to a file.
Also, the parentheses remove any ambiguity of order, especially if you want to pipe stdout and stderr to another command instead.
Edit: To avoid starting a subshell, you instead could use curly braces instead of parentheses to create a group command:
(Note that a semicolon (or newline) is required to terminate the group command.)