|Basic Linux Operations FAQ|
|Part 3 of the Linux Newbie Administrator Guide|
3.4.7 Input/output redirectionThere are three important input-output streams: standard input ("stdin"), standard output ("stdout"), and standard error output ("stderr"). They all default to the console ("console" means the keyboard for the input and the screen for the output), but they can be redirected.
To redirect the standard output I use ">". For example:
dir my_dir > filelisting.txt
will redirect the standard output of the dir command into the textfile filelisting.txt and nothing should appear on my screen. The file can be subsequently edited (e.g. with pico filelisting.txt) or embedded into a document.
To redirect the standard error, I need to use the construct "2>". For example:
dir my_dir 2> errorlisting.txt
The above will send the normal output onto the screen and nothing to the file unless dir produces an error. On error, nothing may go to my screen, and the file errorlisting.txt will contain the error message, which might be something like:
dir: my_dir: Permission denied
Finally, I can redirect both standard output and standard error to a file using:
dir my_dir > file_and_error_listing.txt 2>&1
which first redirects the standard output to a textfile, and then redirects the standard error to the same location as the standard output. A bit twisted, how it works, but it works.
In the examples above, if the file (to which to redirect) already existed, it will be overwritten. To append to an existing file, I use ">>" as in these examples:
If you are
puzzled by the "2>" symbol, here, briefly, is how to rationalize it.
The standard streams have standard descriptors. "0" is standard input,
"1" standard output and "2" is standard error.
is short for
my_dir 1> file.txt
and therefore the example below redirects the standard error:
dir my_dir 2> file.txt
One can also use the symbol "|" to send ("pipe") the output from one command as input for another command. In this popular example, the output from dir is piped to more (more pauses the display after each screenful):
dir | more
One can also split the output so it goes both to a file and the screen using "tee":
dir | tee filelisting.txt
It is called "tee" by the analogy to the "T"-letter-shape fitting that pipefitters use, and which divides flow.
This section so far dealt with redirecting standard output. Redirecting standard input is not nearly as useful as redirecting the output, but it can be done using a construct like this:
cat < my_file
There is also something called in-line redirection of the standard output, realized with "<<". Forget about it, seems of no use to me. Yet, here is an example if you really ever needed it (here, the ">" stands for the secondary prompt):
Apart from redirection to regular files and "filters" (as shown in the examples above), one can redirect to/from devices and other special files. Some examples follow.
An example of redirection to a device file. The following command displays the listing of files on the fourth text terminal:
dir > /dev/tty4
An example of redirection to a special "FIFO" file. This command sends the message "you are lucky" to the lucky ICQ user UIN 77777777 (assuming you are connected to the icq server with your licq program):
echo message 77777777 "you are lucky" > ~/.licq/licq_fifo
The above works because the file "licq_fifo" in your licq directory is a special "fifo" (first-in-first-out) queue file. How could the above ever be more useful than sending a message using the pretty licq GUI front-end? For example, you could write a short script to impress fellow icq users with multiple (identitcal) messages: