363

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.

0

5 Answers 5

574

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
12
  • 179
    And just to save someone else the frustration, note that the order is important: 2>&1 needs to occur after >> log_file.
    – Rufflewind
    Jan 2, 2014 at 6:34
  • 8
    Why >> and not > ? Mar 16, 2016 at 21:38
  • 19
    >> appends to the file, > overwrites. Search for "shell redirection" for more details.
    – Mat
    Mar 17, 2016 at 5:04
  • 5
    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). Mar 1, 2017 at 20:06
  • 17
    and to see the output while it also gets saved to a file: command 2>&1 | tee log_file Aug 25, 2017 at 21:31
272

The simplest syntax to redirect both is:

command &> logfile

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

command &>> logfile
9
  • 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, 2014 at 7:32
  • 12
    @CostiCiudatu the &>> operator does not seem to work in Mac OS X; safer to use Mat's solution imo. May 24, 2014 at 18:28
  • 4
    @JamesFennell You're right, I wasn't aware of that. I upvoted the accepted answer :) May 25, 2014 at 19:10
  • 12
    &> now works as expected on OS X 10.11.1 (seems to be bash 3.2), just for the record. Nov 28, 2015 at 20:32
  • 4
    tldp.org/LDP/abs/html/io-redirection.html mentions this syntax as being functional "as of Bash 4, final release".
    – Dagrada
    Jan 29, 2016 at 18:15
50

You can do it like that 2>&1:

 command > file 2>&1
4
  • 20
    +1, 2>&1 redirects file descriptor 2 (stderr) to file descriptor 1 (stdout)
    – Sjoerd
    Sep 23, 2011 at 9:42
  • 1
    Why do you need an ampersand here? i.e Why is it 2>&1 and not 2>1?
    – btomtom5
    Oct 31, 2019 at 2:50
  • 3
    @btomtom5 & means file descriptor. How would write to a file named 1 otherwise?
    – vstepaniuk
    Apr 27, 2020 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 at 19:52
10

Use:

command >>log_file 2>>log_file
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'

Not the answer you're looking for? Browse other questions tagged or ask your own question.