IO+Redirection

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.

flat

= = =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:



=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. code format="bash" cat file1.txt file2.txt > combined.txt code 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. code format="bash" cat file1.txt file2.txt >> combined.txt code Appends the output to the end of combined.txt. code format="bash" head < file1.txt code Uses file1.txt as the input to execute head which returns the first 10 lines of file1.txt. code format="bash" cat file1.txt file2.txt | head code 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: code format="bash" cat file1.txt file2.txt > combined.txt head combined.txt rm combined.txt code Piping lets you bypass writing the file combined.txt which you probably didn't need.
 * 1) Prints the first 10 lines of file1.txt.
 * 1) Prints the first 10 lines of the combined text from file1.txt and file2.txt.
 * 1) Prints the first 10 lines of the combined text from file1.txt and file2.txt.

=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. code format="bash" python script.py 2>&1 >> log.txt python script.py 2> log.txt 1> /dev/null python script.py | perl script.pl 2>&1 >> log.txt python script.py 2>&1 >> log1.txt | perl script.pl 2>&1 >> log2.txt code
 * 1) in this example both stdout and stderr generated by the
 * 2) Python script are redirected into a log-file
 * 1) here the error messages are redirected into a log file
 * 2) the stdout messages are trashed and don't clutter your screen or the log file
 * 1) an example of logging the output of a pipe
 * 2) you can can also log the messages of each script separately: