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.
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 :
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.
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.
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).
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'.
$ cat >textfile This is my new text file ^D
$ echo "This is my new line" >>file
$ cat file1>>file2
$ cat file1 file2 >file3
The more you'll use Linux the more you'll find Redirection Operators useful.