361

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.

Lee Goddard
  • 10,680
  • 4
  • 46
  • 63
sdmythos_gr
  • 5,444
  • 3
  • 25
  • 25

5 Answers5

568

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
Mat
  • 202,337
  • 40
  • 393
  • 406
  • 177
    And just to save someone else the frustration, note that the order is important: `2>&1` needs to occur after `>> log_file`. – Rufflewind Jan 02 '14 at 06:34
  • 8
    Why `>>` and not `>` ? – user3527975 Mar 16 '16 at 21:38
  • 19
    `>>` appends to the file, `>` overwrites. Search for "shell redirection" for more details. – Mat Mar 17 '16 at 05:04
  • 4
    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)`. – Camille Goudeseune Mar 01 '17 at 20:06
  • 16
    and to see the output while it also gets saved to a file: `command 2>&1 | tee log_file` – Jonathan Hartley Aug 25 '17 at 21:31
  • 1
    What does '&' do in this context? – xaxes Jan 10 '18 at 18:47
  • 1
    @xaxes: the `>&` is a single "token" here. See the "Duplicating File Descriptors" part of the bash man page (or the whole REDIRECTION section). – Mat Jan 10 '18 at 18:53
  • 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 ? – tusharRawat Aug 04 '20 at 16:13
  • 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. – falsePockets Apr 01 '21 at 03:43
  • http://mywiki.wooledge.org/BashFAQ/032 @falsePockets – Mat Apr 01 '21 at 10:45
  • @Rufflewind, thanks! So why does the order matter? Seems counter-intuitive to put the stderr redirection after the stdout redirection. – jschmitter Sep 29 '21 at 19:12
  • `2>&1` can be metaphorically understood as an assignment `stderr_handle = dup(stdout_handle);` in a C-like programming language. – Rufflewind Oct 01 '21 at 00:22
271

The simplest syntax to redirect both is:

command &> logfile

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

command &>> logfile
Costi Ciudatu
  • 37,042
  • 7
  • 56
  • 92
  • 7
    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. – James Wald Apr 10 '14 at 07:32
  • 12
    @CostiCiudatu the &>> operator does not seem to work in Mac OS X; safer to use Mat's solution imo. – James Fennell May 24 '14 at 18:28
  • 4
    @JamesFennell You're right, I wasn't aware of that. I upvoted the accepted answer :) – Costi Ciudatu May 25 '14 at 19:10
  • 1
    For Bash 4+, there is a version of this that will pipe to another command: `|&`. See the comments on [this question](http://stackoverflow.com/a/16497456/1830736). – George Hilliard May 29 '15 at 13:40
  • 12
    `&>` now works as expected on OS X 10.11.1 (seems to be bash 3.2), just for the record. – LiberalArtist Nov 28 '15 at 20:32
  • 4
    http://www.tldp.org/LDP/abs/html/io-redirection.html mentions this syntax as being functional "as of Bash 4, final release". – Dagrada Jan 29 '16 at 18:15
  • I believe for appending in any shell but Bash > v3.2 you need to use: command >> logfile 2>&1 – Eki Aug 31 '16 at 19:02
  • 1
    $(command 2>&1) works in bash 4.3.48(1) but $(command &>) does not. – JohnMudd Aug 05 '17 at 16:46
  • 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! – personal_cloud Oct 02 '17 at 21:06
50

You can do it like that 2>&1:

 command > file 2>&1
Laurent Legrand
  • 1,104
  • 6
  • 6
  • 20
    +1, `2>&1` redirects file descriptor 2 (stderr) to file descriptor 1 (stdout) – Sjoerd Sep 23 '11 at 09:42
  • 1
    Why do you need an ampersand here? i.e Why is it 2>&1 and not 2>1? – btomtom5 Oct 31 '19 at 02:50
  • 3
    @btomtom5 `&` means file descriptor. How would write to a file named `1` otherwise? – vstepaniuk Apr 27 '20 at 19:56
  • 1
    @vstepaniuk 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" – torez233 Jan 14 '23 at 19:52
10

Use:

command >>log_file 2>>log_file
whoan
  • 8,143
  • 4
  • 39
  • 48
blankabout
  • 2,597
  • 2
  • 18
  • 29
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'

PaulDaviesC
  • 1,161
  • 3
  • 16
  • 31