How to redirect both stdout and stderr to a file [duplicate]
Asked Answered
M

5

367

I am running a bash script that creates a log file for the execution of the command

I use the following

Command1 >> log_file
Command2 >> log_file

This only sends the standard output and not the standard error which appears on the terminal.

Mcelroy answered 23/9, 2011 at 9:35 Comment(0)
A
579

If you want to log to the same file:

command1 >> log_file 2>&1

If you want different files:

command1 >> log_file 2>> err_file
Ait answered 23/9, 2011 at 9:37 Comment(12)
And just to save someone else the frustration, note that the order is important: 2>&1 needs to occur after >> log_file.Sooty
Why >> and not > ?Asta
>> appends to the file, > overwrites. Search for "shell redirection" for more details.Ait
A variation that views rather than saves log_file, e.g. when make vomits ten thousand errors that scroll off the screen: vi <(make 2>&1).Unscientific
and to see the output while it also gets saved to a file: command 2>&1 | tee log_fileEhlers
What does '&' do in this context?Lowery
@xaxes: the >& is a single "token" here. See the "Duplicating File Descriptors" part of the bash man page (or the whole REDIRECTION section).Ait
I have file1.txt and file2.txt in currect directory. When I run: 1. ls file{1..5}.txt 1>res.txt 2>&1: it gives correct output to res.txt But when I write 2. ls file{1..5}.txt 1>res.txt 2>res.txt: now it doesn't give correct output to res.txt, here in res.txt some output is omitted compare to what it should've been if we weren't redirect any stream. Why ?Jesusitajet
This doesn't work for me. time echo hi >> e.log 2>&1 the output of time to stderr ends up in the terminal and not the file.Bogle
mywiki.wooledge.org/BashFAQ/032 @BogleAit
@Rufflewind, thanks! So why does the order matter? Seems counter-intuitive to put the stderr redirection after the stdout redirection.Malacology
2>&1 can be metaphorically understood as an assignment stderr_handle = dup(stdout_handle); in a C-like programming language.Sooty
M
283

The simplest syntax to redirect both is:

command &> logfile

If you want to append to the file instead of overwrite:

command &>> logfile
Montague answered 23/9, 2011 at 10:14 Comment(9)
Not sure when this operator was added but it may not be available in older versions of Bash. It does appear to be working on my machine which runs Gnu bash v3.2.48.Protein
@CostiCiudatu the &>> operator does not seem to work in Mac OS X; safer to use Mat's solution imo.Triboluminescence
@JamesFennell You're right, I wasn't aware of that. I upvoted the accepted answer :)Montague
For Bash 4+, there is a version of this that will pipe to another command: |&. See the comments on this question.Keaton
&> now works as expected on OS X 10.11.1 (seems to be bash 3.2), just for the record.Coruscation
tldp.org/LDP/abs/html/io-redirection.html mentions this syntax as being functional "as of Bash 4, final release".Gallager
I believe for appending in any shell but Bash > v3.2 you need to use: command >> logfile 2>&1Codling
$(command 2>&1) works in bash 4.3.48(1) but $(command &>) does not.Bargello
I+1. Finally, bash matches the conciseness of the old command >& logfile in tcsh. It might almost be time for me to switch shells now!Turin
H
50

You can do it like that 2>&1:

 command > file 2>&1
Hydrophobia answered 23/9, 2011 at 9:37 Comment(4)
+1, 2>&1 redirects file descriptor 2 (stderr) to file descriptor 1 (stdout)Palanquin
Why do you need an ampersand here? i.e Why is it 2>&1 and not 2>1?Dereism
@Dereism & means file descriptor. How would write to a file named 1 otherwise?Backer
@Backer I guess this depends on the context? Before ">" we don't need & to denote file descriptor because in this context only file descriptor is possible so & is implicit. After ">" the destination can be anything, so we have to use explicit & to specify that we are referring to a file descriptor rather than a file named "1"Hilariahilario
S
10

Use:

command >>log_file 2>>log_file
Svetlana answered 23/9, 2011 at 9:37 Comment(0)
S
0

Please use command 2>file Here 2 stands for file descriptor of stderr. You can also use 1 instead of 2 so that stdout gets redirected to the 'file'

Sparrow answered 24/9, 2011 at 5:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.