Skip to content

            Lost ?  |  Need an account:
 
Home >> Knowledge Base >> Operating Systems >> Linux >> Stream Redirection - Standard Error
Stream Redirection - Standard Error 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

Standard Error or STDERR


STDERR can also be thought of as a output container, but different from STDOUT in that its contents are of errors from the running process.  Also like STDOUT, STDERR usually defaults it's output to a screen buffer device like the console or a X terminal. Redirecting STDERR can also be useful for a variety of tasks; we will not go into all of them, but will highlight a couple examples to give you an idea.

When running find on the root of your file-system as a non-privileged user, your console will often times become polluted with Permission denied errors making it very difficult to sift through the output distinguishing the search string from the errors:

$ find / -name some_filename.txt -print
find: /usr/lib64/audit: Permission denied
find: /usr/libexec/utempter: Permission denied
find: /lost+found: Permission denied

......Output Truncated.......

We know that we don't have permissions to all of these folders, but for the ones we do, we would like find to search in them but less all the permission denied errors.  We can do this by the use of the STDERR output modifier 2>.  Using the STDERR output modifier, we can now choose to output all the errors to a text file (for further analysis) or we can just discard them altogether.  In the first example, we'll send the errors to a text file:

$ find / -name some_filename.txt -print 2> error_log.txt
/home/thirt/some_filename.txt

$

As you can see, running find this time produced a much more clean output that only shows the exact hits of our search criteria.  If we like, we can now go back and exam the contents of error_log.txt to see if there was anything of further interest.  We could have also just as easily discarded the output as seen in our next example:

$ find / -name some_filename.txt -print 2> /dev/null
/home/thirt/some_filename.txt

$

Here, we sent all the errors to the black hole, /dev/null.  This can be useful when you know the command you are running will generate errors but you do not wish to see them.

Appending STDERR


Just like STDOUT, we can use the STDERR append modifier 2>> if we would like the output of our command to be appended to a file rather than overwriting the contents.  For example:

$ ls NO_ACCESS_TO_THIS_DIR 2>> error_log.txt

$ find . -name some_filename.txt 2>> error_log.txt
./some_filename.txt

$ cat error_log.txt
ls: NO_ACCESS_TO_THIS_DIR: Permission denied
find: ./NO_ACCESS_TO_THIS_DIR: Permission denied

$

From the above example, we see our first command ls completes with no output sent to the screen and STDERR sent to error_log.txt.  The second command, find, sends the results of the search to our screen (STDOUT) and any errors (STDERR) to error_log.txt.  Upon further inspection of error_log.txt, we see both commands threw errors but because we redirected STDERR, we never saw them until we inspected the contents of error_log.txt.



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: 83
Members Online: 1