Skip to content

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

Standard Output or STDOUT


STDOUT can be thought of as the output container produced by a given process.  Most programs will usually default their STDOUT to a screen buffer device such as the system console or a terminal like your X terminal.  So for example, when running the command ls, the output that is produces is sent to our connected X terminal:

$ ls
command_list  some_file.txt

So in the example above, we see a simple directory listing of our working directory.  The highlighted output from 'ls' was sent to STDOUT which happened to be our X terminal.

We can now redirect the output of ls to a file by the use of our output modifier 1>.  When using a output modifier, if the target file already exists, its contents will be overwritten.  If the target does not exist, a new file will be created.

$ ls 1> file_list.txt

$ echo $?
0

$ cat file_list.txt
command_list
file_list.txt
some_file.txt

As you can see, the output which had previously be sent to our screen was redirected to a newly created file called file_list.txt. Like input redirection, we can drop the 1 from our syntax to simply the command.

$ ls > file_list.txt

$ cat file_list.txt
command_list
file_list.txt
some_file.txt

Rerunning a command with the > will only overwrite the contents of what had previously been stored in the target file:

$ echo "one" > file_list.txt

$ echo "two" > file_list.txt

$ echo "three" > file_list.txt

$ cat file_list.txt
three


Appending STDOUT


Appending of STDOUT to a target can be a very valuable tool to you for logging and other such utilities on a system.  In the above example, with each iteration of the command echo, the targeted output's contents were replaced with the commands most recent output.  You can append the output to STDOUT by the use of >> as the output modifier.  For example:

$ cat /dev/null > file_list.txt

$ echo "First Time:" >> file_list.txt

$ ls >> file_list.txt

$ echo "Second Time:" >> file_list.txt

$ ls >> file_list.txt

$ cat file_list.txt
First Time:
command_list
file_list.txt
some_file.txt
Second Time:
command_list
file_list.txt
some_file.txt

In the above, we began by zeroing out the file using the overwrite output modifier, '>'.  We then append, '>>' to the text "First Time:" followed by a directory listing, followed by the text "Second Time:" followed by a directory listing.  I've highlighted the output to help illustrate this example.

Although the above was a trivial example, in the following diagram, we've created a simple file monitoring program that makes use of the date command to log when a file trigger_file appears on the system. Lets take a look:

$ infant_loop=0

$ while [ $infant_loop -eq 0 ]; do
if [ -e trigger_file ]; then
echo -n `date` >> logfile.txt;
echo ' - trigger_file found on system, will now clean-up' >> logfile.txt;
rm -rf ./trigger_file;
fi; sleep 1;
done &

[1] 23809

$ touch trigger_file

$ touch trigger_file

$ touch trigger_file

$ kill -9 23809

$ cat trigger_file
Tue Feb 24 17:58:13 EST 2009 - trigger_file found on system, will now clean-up
Tue Feb 24 18:01:54 EST 2009 - trigger_file found on system, will now clean-up
Tue Feb 24 18:02:26 EST 2009 - trigger_file found on system, will now clean-up

Still a very basic example, but you can now begin to see how powerful stream redirection can be.



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