Saturday, January 23, 2016

To redirect both stdout output and stderr error to a file

0: stdin
1: stdout
2: stderr

Note that the order of redirections is significant. For example, the command

              ls > dirlist 2>&1

directs both standard output and standard error to the file dirlist, while the command

              ls 2>&1 > dirlist

directs only the standard output to file dirlist, because the standard error was duplicated from the standard output before the standard output was redirected to dirlist.

Redirecting Standard Output and Standard Error:

This construct allows both the standard output (file descriptor 1) and the standard error output (file descriptor 2) to be redirected to the file whose name is the expansion of word.

There are two formats for redirecting standard output and standard error:




Of the two forms, the first is preferred. This is semantically equivalent to

              >word 2>&1

To redirect both stdout output and stderr error to a file:

# find . &> /tmp/test.txt

Redirects STDOUT to log and than replaces STDERR with the redirected STDOUT:

# some_cmd > log 2>&1

Replaces STDERR with STDOUT and then redirects the original STDOUT to log:

# some_cmd 2>&1 > log

Lookup the manual and find keyword redirection for detail:

# man bash


No comments: