Understanding:
By default, Linux commands obtain input from the standard input device (stdin) and output the result to the standard output device (stdout) for display. Generally, the standard input device is the keyboard, and the standard output device is the terminal, that is, the display. When running commands in Linux Shell, each process is associated with three open files and uses file descriptors to reference these files. Because the file descriptor is not easy to remember, shell also provides the corresponding file name:
File descriptor description list
File |
File descriptor |
Input File-standard input |
0 (Keyboard by default; 0 is the output of files or other commands) |
Output file-standard output |
1 (screen by default; 1 is a file) |
Error output file-standard error |
2 (screen by default; 2 is a file) |
List of all available command lines
Command |
Description |
Command> File |
Redirects the output to file. |
Command <File |
Redirects the input to file. |
Command> File |
Redirects the output to file in append mode. |
N> File |
Redirects a file whose file descriptor is n to a file. |
N> File |
Redirects an object whose file descriptor is n to a file in append mode. |
N> & M |
Merge the output files M and N. |
N <& M |
Merge the input files M and N. |
<Tag |
The content between the start tag and end tag is used as the input. |
By default, command> file redirects stdout to file, and command <file redirects stdin to file.
- If you want stderr to redirect to file, you can write:
$ Command 2> file #2 indicates a standard error file (stderr)
- If you want stderr to append to the end of the file, you can write:
$ Command 2> file #>> indicates appending and writing, without overwriting the previous content.
- If you want to merge stdout and stderr and redirect to file, you can write:
$ command > file 2>&1
Or
$ command >> file 2>&1
- If you want to redirect both stdin and stdout, you can write:
$ Command <infile> OUTFILE # command redirects stdin to infile and stdout to OUTFILE