- How to redirect stderr to a file [duplicate]
- 2 Answers 2
- Shell redirections
- Commands writing to stderr exclusively
- Working with Input Output and Error Redirection in Linux
- Redirecting Standard Output
- Redirecting a Command’s Input
- Redirecting Standard Error
- Redirecting both Standard Output & Standard Error
- Redirecting Both stderr & stdout at Once
- Appending To Files
- Truncating Files :
- Sending Output to Nowhere Fast
- How to redirect and append both standard output and standard error to a file with Bash
- 8 Answers 8
- Here comes some additional tips.
- How do I save terminal output to a file?
- 9 Answers 9
- Overview:
How to redirect stderr to a file [duplicate]
While using nohup to put a command to run in background some of content appear in terminal.
I want to save that content to a file.
2 Answers 2
There are two main output streams in Linux (and other OSs), standard output (stdout) and standard error (stderr). Error messages, like the ones you show, are printed to standard error. The classic redirection operator ( command > file ) only redirects standard output, so standard error is still shown on the terminal. To redirect stderr as well, you have a few choices:
Redirect stdout to one file and stderr to another file:
Redirect stdout to a file ( >out ), and then redirect stderr to stdout ( 2>&1 ):
Redirect both to a file (this isn’t supported by all shells, bash and zsh support it, for example, but sh and ksh do not):
For more information on the various control and redirection operators, see here.
First thing to note is that there’s couple of ways depending on your purpose and shell, therefore this requires slight understanding of multiple aspects. Additionally, certain commands such as time and strace write output to stderr by default, and may or may not provide a method of redirection specific to that command
Basic theory behind redirection is that a process spawned by shell (assuming it is an external command and not shell built-in) is created via fork() and execve() syscalls, and before that happens another syscall dup2() performs necessary redirects before execve() happens. In that sense, redirections are inherited from the parent shell. The m&>n and m>n.txt inform the shell on how to perform open() and dup2() syscall (see also How input redirection works, What is the difference between redirection and pipe, and What does & exactly mean in output redirection )
Shell redirections
Most typical, is via 2> in Bourne-like shells, such as dash (which is symlinked to /bin/sh ) and bash ; first is the default and POSIX-compliant shell and the other is what most users use for interactive session. They differ in syntax and features, but luckily for us error stream redirection works the same (except the &> non standard one). In case of csh and its derivatives, the stderr redirection doesn’t quite work there.
Let’s come back to 2> part. Two key things to notice: > means redirection operator, where we open a file and 2 integer stands for stderr file descriptor; in fact this is exactly how POSIX standard for shell language defines redirection in section 2.7:
For simple > redirection, the 1 integer is implied for stdout , i.e. echo Hello World > /dev/null is just the same as echo Hello World 1>/dev/null . Note, that the integer or redirection operator cannot be quoted, otherwise shell doesn’t recognize them as such, and instead treats as literal string of text. As for spacing, it’s important that integer is right next to redirection operator, but file can either be next to redirection operator or not, i.e. command 2>/dev/null and command 2> /dev/null will work just fine.
The somewhat simplified syntax for typical command in shell would be
The trick here is that redirection can appear anywhere. That is both 2> command [arg1] and command 2> [arg1] are valid. Note that for bash shell, there there exists &> way to redirect both stdout and stderr streams at the same time, but again — it’s bash specific and if you’re striving for portability of scripts, it may not work. See also Ubuntu Wiki and What is the difference between &> and 2>&1.
Note: The > redirection operator truncates a file and overwrites it, if the file exists. The 2>> may be used for appending stderr to file.
If you may notice, > is meant for one single command. For scripts, we can redirect stderr stream of the whole script from outside as in myscript.sh 2> /dev/null or we can make use of exec built-in. The exec built-in has the power to rewire the stream for the whole shell session, so to speak, whether interactively or via script. Something like
In this example, the log file should show stat: cannot stat ‘/etc/non_existing_file’: No such file or directory .
Yet another way is via functions. As kopciuszek noted in his answer, we can write function declaration with already attached redirection, that is
Commands writing to stderr exclusively
Commands such as time and strace write their output to stderr by default. In case of time command, the only viable alternative is to redirect output of whole command , that is
alternatively, synchronous list or subshell could be redirected if you want to separate the output ( as shown in related post ):
Other commands, such as strace or dialog provide means to redirect stderr. strace has -o option which allows specifying filename where output should be written. There is also an option for writing a textfile for each subprocess that strace sees. The dialog command writes the text user interface to stdout but output to stderr, so in order to save its output to variable ( because var=$(. ) and pipelines only receives stderr ) we need to swap the file descriptors
but additionally, there is —output-fd flag, which we also can utilize. There’s also the method of named pipes. I recommend reading the linked post about the dialog command for thorough description of what’s happening.
Источник
Working with Input Output and Error Redirection in Linux
Every process in Linux is provided with three open files( usually called file descriptor). These files are the standard input, output and error files. By default :
- Standard Input is the keyboard, abstracted as a file to make it easier to write shell scripts.
- Standard Output is the shell window or the terminal from which the script runs, abstracted as a file to again make writing scripts & program easier
- Standard error is the same as standard output:the shell window or terminal from which the script runs.
A file descriptor is simply a number that refers to an open file. By default , file descriptor 0 (zero) refers to the standard input & often abbreviated as stdin. File descriptor 1 refers to standard output (stdout) and file descriptor 2 refers to standard error (stderr). These numbers are important when you need to access a particular file , especially when you want to redirect these files to the other locations, File descriptors numbers go up from zero.
Redirecting Standard Output
Syntax to redirect the output of a command to a file.
We can see the data that would have gone to the screen with more command :
The > operator tells the shell to redirect the output of the command to the given file. If the file exists , the deletes the old contents of the file and replaces it with the output of the command.
Redirecting a Command’s Input
Syntax to redirect the input of a command to come from a file.
Use the Note : We can also combine both redirections with following syntax :
Redirecting Standard Error
In addition to redirecting the standard input and output for a script or a command, we can also redirect standard error. Even though standard error by defaults goes to the same place as the standard output – the shell window or terminal. There are good reasons why stdout and stderr are treated separately. The main reason is that we can redirect the output of a command or commands to a file but you have no way of knowing whether an error occurred. Separating stderr from stdout allows the error message to appear on your screen while output still goes to a file.
Syntax to redirect stderr from a command to a file.
# command_options_and_agruments 2> output_file.
The 2 in 2> refers to the file descriptor 2, the descriptor number for stderr.
Redirecting both Standard Output & Standard Error
Use 2>&1 Syntax to redirect standard error to the same location as standard output .
Above Command has three parts.
- ls /usr/bin is the command run
- > command.txt redirects the output of the ls command
- 2>&1 sends the output of the file descriptor 2, stderr , to the same location as the file descriptor 1, stdout.
Note that above example assumes that your system doesn’t have directory names “/usr2222/bin”
Redirecting Both stderr & stdout at Once
In the above command ls is the command , /usr2222/bin is the argument to the ‘ls‘ command and ‘&> command.txt‘ redirect both stdout and stderr to a file named command.txt.
Appending To Files
Use the ‘>>’ operator to redirect the output of a command , but append to the file , if it exists. The syntax is given below :
Truncating Files :
We can use a shorthand syntax for truncating files by omitting the command before > operator . The Syntax is given below :
We can also use an alternate format with a colon :
Both of these command-less command will create the file if it does not exist and truncate the file to zero bytes if the file does exist.
Sending Output to Nowhere Fast
There are some scenarios where you not only want to redirect the output of a command , you want to throw the output away. You can do this by redirecting a command’s output to the null file “/dev/null” The null file consumes all output sent to it , as if /dev/null is a black hole star.
Note : The file /dev/null is often called a bit bucket.
Источник
How to redirect and append both standard output and standard error to a file with Bash
To redirect standard output to a truncated file in Bash, I know to use:
To redirect standard output in Bash, appending to a file, I know to use:
To redirect both standard output and standard error to a truncated file, I know to use:
How do I redirect both standard output and standard error appending to a file? cmd &>> file.txt did not work for me.
8 Answers 8
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 Z shell ( zsh ) 4.3.11):
just out of box.
This should work fine:
It will store all logs in file.txt as well as dump them in the terminal.
Your usage of &> x.file does work in Bash 4. Sorry for that: (
Here comes some additional tips.
0, 1, 2, . 9 are file descriptors in bash.
0 stands for standard input, 1 stands for standard output, 2 stands for standard error. 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: >
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 standard error to standard output.
- Redirect the new standard output by appending to a file.
Also, the parentheses remove any ambiguity of order, especially if you want to pipe standard output and standard error to another command instead.
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.)
Источник
How do I save terminal output to a file?
How do I save the output of a command to a file?
Is there a way without using any software? I would like to know how.
9 Answers 9
Yes it is possible, just redirect the output (AKA stdout ) to a file:
Or if you want to append data:
If you want stderr as well use this:
or this to append:
if you want to have both stderr and output displayed on the console and in a file use this:
(If you want the output only, drop the 2 above)
To write the output of a command to a file, there are basically 10 commonly used ways.
Overview:
Please note that the n.e. in the syntax column means «not existing».
There is a way, but it’s too complicated to fit into the column. You can find a helpful link in the List section about it.
The standard output stream will be redirected to the file only, it will not be visible in the terminal. If the file already exists, it gets overwritten.
The standard output stream will be redirected to the file only, it will not be visible in the terminal. If the file already exists, the new data will get appended to the end of the file.
command 2> output.txt
The standard error stream will be redirected to the file only, it will not be visible in the terminal. If the file already exists, it gets overwritten.
command 2>> output.txt
The standard error stream will be redirected to the file only, it will not be visible in the terminal. If the file already exists, the new data will get appended to the end of the file.
Both the standard output and standard error stream will be redirected to the file only, nothing will be visible in the terminal. If the file already exists, it gets overwritten.
Both the standard output and standard error stream will be redirected to the file only, nothing will be visible in the terminal. If the file already exists, the new data will get appended to the end of the file..
command | tee output.txt
The standard output stream will be copied to the file, it will still be visible in the terminal. If the file already exists, it gets overwritten.
command | tee -a output.txt
The standard output stream will be copied to the file, it will still be visible in the terminal. If the file already exists, the new data will get appended to the end of the file.
Bash has no shorthand syntax that allows piping only StdErr to a second command, which would be needed here in combination with tee again to complete the table. If you really need something like that, please look at «How to pipe stderr, and not stdout?» on Stack Overflow for some ways how this can be done e.g. by swapping streams or using process substitution.
command |& tee output.txt
Both the standard output and standard error streams will be copied to the file while still being visible in the terminal. If the file already exists, it gets overwritten.
command |& tee -a output.txt
Both the standard output and standard error streams will be copied to the file while still being visible in the terminal. If the file already exists, the new data will get appended to the end of the file.
Источник