Linux IO redirection
By default, Linux commands obtain input from the standard input device (stdio) and output the result to the standard output device (stdout) for display. Generally, the standard input device is a keyboard. The standard output device is a terminal (Display ).
Generally, three files are opened when each command is run:
- Standard input file (stdin): The file descriptor of stdin is 0. By default, linux programs read data from stdin.
- Standard output file (stdout): The file descriptor of stdout is 1. The linux program outputs data in comparison to stdout.
- Standard Error file (stderr): The file descriptor of stderr is 2. The linux program writes error messages to the stderr stream.
Output redirection
/Dev/null File
/Dev/null is a special file, and the content written into it will be discarded.
Example:
$ Date> date.txt convert the result of the datecommand to the file date.txt $ ll> date.txt then add the result of the llcommand to the file date.txt $ cat <date.txt then redirect the input of the catcommand to the file date.txt. (That is, the content of the file date.txt is used as the cat parameter) $ cat <date.txt> date2.txt redirects the content of the file date.txt to $ cat <EOF # prints the input string from the keyboard to the display until the input EOF $. /test 2> result.txt # redirects the error message of the script test to the file result $. /test> result.txt 2> & 1 export the error information and output information of the script test to the result.txt file.
This article permanently updates the link address: