===== Input, Output and Error Redirection =====
====1. Introduction====
When you run a **UNIX** command usually that program sends an output to the shell prompt. If you don't want your output to be visible in your shell prompt, you can redirect it. You could make the output go directly to the printer, into a file or you can make it vanish .
This is called : //Redirection//. You can also redirect the input for a program.
====2. File Descriptors====
Everything in **UNIX** is stored as files, even the devices that you have connected to your machine. Just like everything else,
your screen on which programs send their output is also a file for **UNIX**. So when a program is executed it sends the output to a //'file descriptor'// which by default is your screen. If the file descriptor would be a regular file, then the output would be sent to the file.
When a program/command is executed, there are 3 files for the process to work with : //Standard Input//, //Standard Output// and //Standard Error// :
* **0** - __Standard Input__ (//Keyboard//)
* **1** - __Standard Output__ (//Screen//)
* **2** - __Standard Error__ (//Screen//)
You can redirect any of these files to other files. For example, if you redirect the //Standard Output// (1) to the **printer**, it will start printing your programs output, instead of sending it to the **screen**.
====3. Output Redirection====
__Output Redirection__ is the most common used **Redirection**. It is used to redirect the output of a command to a file.Usually this is used when your command generates a lot of output. To redirect this output you have to use the //'>'// operator. This is called the //"output redirection operator"//. For example :
# ls -al > files
The above command will redirect the output of the **ls** command into a file called //"files"//, so the result that you usually expect to be printed on the screen is in the file called //"files"//.
__Warning__: If the file //"files"// already exists, it will be overwritten. If you want to append results to the same file you have to use the //'>>'// operator. This way you won't lose the contents of your file.
====4. Input Redirection====
__Input Redirection__ is not as common as the //Output Redirection//, but when needed, it cand be of great use. It is used if you have some kind of file, and you wish to run a specific command on that file .
**Input Redirection** is done with help from the //'<'// operator. See the example below:
$ mail root < my_message
This command will start the //mail// program and will use the input from the file called //"my_message"//
__Note__: You can only use //input redirection// with programs that expect __input__ from you when executed (just like //Output Redirection// is only useful with programs the have //output// once started).
====5. Error Redirection====
This is a widely used **UNIX** feature. For example, if you are a **UNIX** user and perform the //'find'// command you should usually get a lot of //'permission denied'// error messages. It is very simple to redirect these error messages so that it doesn't bother you. __Error Redirection__ is done using the error output file descriptor along with the output redirection operator.So this will look like //'2>'//
Look at the example below :
$ find / -name s*.jpg 2>/dev/null
**/dev/null** in **Linux** is some kind of black hole. What is sent into **/dev/null** will never return. So as you probably think, the above command will send all the //'permission denied'// error messages, along with other messages that might occur, to **/dev/null**.
Here is another example:
$ program 2>errors_generated
The above command will redirect all error messages generated by the execution of //'program'// into a file called //'errors_generated'//.
====6. Other useful ways of using the Redirection Operators.====
* Let's say you want to create a text file very quickly. You can do this without using any editor.
$ cat >textfile
This is my new text file
^D
* Now let's suppose you want to add a single line to an existing file :
$ echo "This is my new line" >>file
* Maybe you want to join to files together. The contents of file1 will be added to file2. This is an easy way of doing that:
$ cat file1>>file2
* You could easily join a couple of files together :
$ cat file1 file2 >file3
The more you'll use **Linux** the more you'll find //Redirection Operators// useful.