Skip to content

            Lost ?  |  Need an account:
 
Home >> Knowledge Base >> Operating Systems >> Linux >> Stream Redirection - Combined Output
Stream Redirection - Combined Output PDF Print E-mail
(2 votes, average 5.00 out of 5)
Written by Tom Hirt   
Tuesday, 24 February 2009 13:06
Article Index
Stream Redirection
Standard Input
Standard Output
Standard Error
Combined Output
All Pages

Combined Output


The final part of our stream redirection discussion focuses on combined out.  Leveraging what we have learned thus far, we may now combine both the STDOUT and STDERR streams into one file. A typical use case for this is system logging.  We often see jobs scheduled through crond or at that redirect their output to STDOUT and/or STDERR for historical records of the transpired events.  This can prove to be a very helpful tool when trying to understand why last night's batch process failed to run properly.

In this example, we look at a basic syntax to combine both STDOUT and STDERR into a single file:

$ find . -name some_filename.txt 2>> error_log.txt 1>> error_log.txt
$ cat error_log.txt
./some_filename.txt
find: ./NO_ACCESS_TO_THIS_DIR: Permission denied
$

Notice the use of our append modifiers >> for both STDOUT and STDERR.  Had we forgotten to use the append modifier, we would have lost the output from STDOUT.  When combining the redirection for STDOUT and STDERR, STDOUT is always written first followed by STDERR.  So depending on if you used the append or overwrite modifier for STDERR, your results will be quite different.  I'll demonstrate with a couple examples.

STDOUT  =  write,  STDERR = append:

$ find . -name some_filename.txt 1> error_log.txt 2>> error_log.txt
$ cat error_log.txt
./some_filename.txt
find: ./NO_ACCESS_TO_THIS_DIR: Permission denied
$

STDOUT = write,  STDERR = write:

$ find . -name some_filename.txt 1> error_log.txt 2> error_log.txt
$ cat error_log.txt
find: ./NO_ACCESS_TO_THIS_DIR: Permission denied
$

STDOUT = append,  STDERR = write:

$ find . -name some_filename.txt 1>> error_log.txt 2> error_log.txt
$ cat error_log.txt
find: ./NO_ACCESS_TO_THIS_DIR: Permission denied
$

So what we have learned, in order to preserve the output of both STDOUT and STDERR in a combined redirection, the STDERR modifier must be set to append or the contents of STDOUT will be lost.

Now although the above examples have helped us better understand the roll of the STDOUT and STDERR modifiers, we had to type a lot more than what is necessary.  Lets lose some keystrokes and simplify our syntax.  We'll start by using a combined output that will overwrite the target file's contents:

$ find . -name some_filename.txt > error_log.txt 2>&1
$ cat error_log.txt
./some_filename.txt
find: ./NO_ACCESS_TO_THIS_DIR: Permission denied

Our syntax from above with use of the single > followed by the file-name and then a 2>&1 tells our interpreter to combine both STDOUT and STDERR, and overwrite the contents of our target file with their output.  For our final example, we combine both STDOUT and STDERR, but this time, appending to the contents of our target file:

$ echo "THIS FILE HAS CONTENT" > error_log.txt
$ find . -name some_filename.txt >> error_log.txt 2>&1
$ cat error_log.txt
THIS FILE HAS CONTENT
./some_filename.txt
find: ./NO_ACCESS_TO_THIS_DIR: Permission denied
$

As expected, the contents of the file were not lost as we used the append modifier, >>, to combine the output into the existing file.

This concludes our article on Stream Redirection, we hope you found this helpful!


Add this page to your favorite website
AddThis Social Bookmark Button


Comments
Add New Search
Write comment
Name:
Email:
 
Website:
Title:
UBBCode:
[b] [i] [u] [url] [quote] [code] [img] 
 
 
:D:):(:0:shock::confused:8):lol::x:P:oops::cry::evil::twisted::roll::wink::!::?::idea::arrow:
 
Please input the anti-spam code that you can read in the image.

!joomlacomment 4.0 Copyright (C) 2009 Compojoom.com . All rights reserved."

Last Updated on Monday, 02 March 2009 15:57
 

Forum Activity

Author: Cogterrit
May.18.12
Author: Cogterrit
May.18.12
Author: Cogterrit
May.18.12
Author: Cogterrit
May.17.12

Online Stats

Guests Online: 81
Members Online: 1