Redirection

Shell redirection operators are used to direct a program's input and output. These operators can be used to feed a program input from another application, from the command line, or even from another file. Those operators are >, >>, and < (there's another redirection operator << which is known as here operator. use this to tell the shell when to stop reading input).

The > operator is known as standard output redirection operator. When used with a program that display output, the output can be redirected to somewhere else than the standard output. For example when used with the cat command (cat is short for concatenate, is used to print one or more files to your display), the cat command will copy a file by sending its output to another file

cat first.txt > second.txt

The output of the example above will be sent to the second.txt in the current directory.

The < (standard input) redirection operator feeds information to a program as follows

cat <>

In the example above, the cat command reads the contents of the file.txt and sends its content to the standart display.

The >> redirection operator is used to append the output of an executed command to a file. The example bellow will redirect the output to the file.txt. If the file.txt doesn't exist, it will be created. If it exists, the output will be appended to the end of the file.txt.

cat first.txt >> file.txt

The standard input and output have assigned file numbers in the shell. The standard input uses the number 0, the standar output uses the number 1 while another type of output is standard error uses the number 2. Most linux command report the error to the standard error output. You can redirect the error reports to a file by doing so:

cat first.txt >> file.txt  2 >> error.log

0 comments:

top