You may not always want to see things dumped onto the screen or you may sometimes want to smoothly combine several commands, so that you don't have to execute them all consecutively by hand. Here are some ways to accomplish this.

| Introduction to the Standard Streams | Input/Output Redirection | Creating Logs

Introduction to the Standard Streams

In UNIX the standard I/O streams are referred to in the following way:
  • 0 standard input
  • 1 standard output
  • 2 standard error

Standard streams flow as follows:

535px-Stdstreams-notitle.svg.png

Input/Output Redirection

Redirection is used to direct the output for a command into a file or to use a file or output from another command as input for a new command. Below are some redirection examples using cat and head.
cat file1.txt file2.txt > combined.txt
Executes cat and places the output (the contents of file1.txt concatenated with the contents of file2.txt) in combined.txt. Note that this will overwrite combined.txt if this file already exists.
cat file1.txt file2.txt >> combined.txt
Appends the output to the end of combined.txt.
head < file1.txt
# Prints the first 10 lines of file1.txt.
Uses file1.txt as the input to execute head which returns the first 10 lines of file1.txt.
cat file1.txt file2.txt | head
# Prints the first 10 lines of the combined text from file1.txt and file2.txt.
The pipe operator ( | ) pipes the output from the command which proceeds it to the command following it. In this example the concatenated text from file1.txt and file2.txt is piped into head returning the first 10 lines of the resulting combined text. Essentially this is the same as:
cat file1.txt file2.txt > combined.txt
head combined.txt
# Prints the first 10 lines of the combined text from file1.txt and file2.txt.
rm combined.txt
Piping lets you bypass writing the file combined.txt which you probably didn't need.

Creating Logs

Having stdout and stderr directed to the display of the computer is not always useful. The displayed messages may contain important information about how the program performed and you may want to have a record of this for later. You can redirect both stdout and stderr into files to log the messages that were generated by the programs you ran.
python script.py 2>&1 >> log.txt
## in this example both stdout and stderr generated by the
## Python script are redirected into a log-file
python script.py 2> log.txt 1> /dev/null
## here the error messages are redirected into a log file
## the stdout messages are trashed and don't clutter your screen or the log file
python script.py | perl script.pl 2>&1 >> log.txt
## an example of logging the output of a pipe
## you can can also log the messages of each script separately:
python script.py 2>&1 >> log1.txt | perl script.pl 2>&1 >> log2.txt