About cmd command redirection output 2> & 1
Mycommand> mylog.txt 2> & 1 should be the most classic usage.
Command results can be output in the form of "%>", % indicates the file descriptor: 1 indicates the standard output stdout, and 2 indicates the standard error stderr. The default value of % is 1, that is, "1>", and 1> can be abbreviated as>, that is, the default value is>. The default destination of stdout is terminal, and the default destination of stderr is terminal. Execute echo text> result.txt in batch processing, and we can see Echo text 1> result.txt on the screen.
In this example, You Need To directly use the redirection symbol.
Application instance:
1st worker outputs the result to result.txt
Net stop myservices> result 2> & 1
2. Hide program output results
Net stop myservices> NUL 2> NUL
Microsoft's article on redirection: using the command redirection Operator
You can use the redirection operator to redirect command input and output data streams from the default location to other locations. The input or output data stream location is called a handle.
The following table lists available handles.
Description of the numeric code of the handle
Stdin
0
Keyboard Input
Stdout
1
Output to Command Prompt window
Stderr
2
Error output to Command Prompt window
Undefined
3-9
Handles are defined separately by applications. They are unique to each tool.
Numbers 0 to 9 represent the first 10 handles. You can run the program using cmd.exe and redirect any of the first 10 handles of the program. To specify the handle to use, enter the number of the handle before the redirection operator. If no handle is defined, the default <redirect input operator is 0, and the default> redirect output operator is 1. After you type the <or> operator, you must specify the data read/write location. You can specify a file name or other existing handles.
To specify the redirection to an existing handle, use the (&) character followed by the handle number (that is, the & handle number) to be redirected ). For example, the following command can redirect handle 2 (stderr) to handle 1 (stdout ):
2> & 1
The following table lists operators that can be used to redirect input and output data streams.
Redirect operator description
>
Write command output to a file or device (such as a printer) instead of a command prompt window or handle.
<
Reads command input from a file instead of a keyboard or handle.
>
Add the command output to the end of the file without deleting the existing information in the file.
> &
Write the output of one handle to the input of another handle.
<&
Read the input from one handle and write it into another handle output.
|
Read the output from one command and write it into the input of another command. It is also called an MPS queue.
By default, the command input (that is, the stdin handle) can be sent to cmd.exe from the keyboard, and then the command output (that is, the stdout handle) is sent to the Command Prompt window by cmd.exe.
Redirect input (<)
To redirect input to a file or device through a keyboard, use the <operator. For example, to obtain the input of the sort command from file.txt, type:
Sort <file.txt
The contents of file.txt are displayed in the Command Prompt window in alphabetical order.
<Operator can open a specified file name with read-only access permission. Therefore, you cannot write information to the file when using this operator. For example, if you start the program with <& 2, all operations that attempt to read handle 0 will fail, because handle 2 is initially opened in write-only access mode.
Note:
0 is the default handle of the redirection input operator.
Redirect output (>)
Almost all commands send the output to the Command Prompt window. Messages and prompts are displayed in the Command Prompt window even if the command output is sent to the drive or printer.
To redirect the output from the command prompt window to a file or device, use the> operator. This operator can be used in many commands. For example, to redirect the Dir output to dirlist.txt, type:
Dir> dirlist.txt
If dirlist.txt does not exist, cmd.exe creates the file. If dirlist.txt exists, cmd.exe replaces the information in the file with the output of the Dir command.
To run the netsh routing dump command and send the output to route. cfg, type:
Netsh routing dump> C: \ route. cfg
> The operator can open a specified file with write-only permission. Therefore, you cannot use this operator to read files. For example, if you use the redirection operator> & 0 to start the program, all attempts to write handle 1 will fail, because handle 0 was initially opened in read-only access mode.
Note:
1 Yes> default handle of the redirection output operator.
Copy handle
The redirection operator can copy the output or input from one specified handle to another. For example, to send the Dir output to file.txt and send the error output to file.txt, type:
Dir> C: \ file.txt 2> & 1
When copying a handle, you can copy all the features of the handle in its original state. For example, if a handle has the read-only access attribute, all copies of the handle have the read-only access attribute. You cannot copy a handle with read-only access attributes to another handle with write-only access attributes.
Use & operator to redirect input and copy
To use the redirected input operator (<) with the copy operator (&), the specified file must already exist. If the input file exists, cmd.exe will open the file in read-only mode, and then send the characters contained in the file as input to this command (as input from the keyboard ). If a period is specified, cmd.exe copies the specified handle to the existing handle of the system.
For example, to open file.txt in the form of reading (stdin) with the handle 0 input, type:
<File.txt
To open file.txt and send the output to the Command Prompt window (stdout) after the content is sorted, type:
Sort <file.txt
To find file.txt, and redirect the handle 1 (stdout) and handle 2 (stderr) to search.txt, type:
Findfile file.txt> search.txt 2 <& 1
To copy the User-Defined handle 3 by reading (stdin) with the handle 0 input, type:
<& 3
Use & operator to redirect output and copy
If the output is redirected to a file and the current file name is specified, cmd.exe will open the file in write-only mode and overwrite the file content. If you specify a period, cmd.exe copies the file to the existing handle.
To copy custom handle 3 to handle 1, type:
> & 3
To redirect all output including handle 2 (that is, stderr) from the ipconfig command to handle 1 (that is, stdout), and then redirect the output to output. Log, type:
Ipconfig.exe> output. log 2> & 1
Use> redirect operator to append the output
To add the output to the end of the file without losing any existing information in the file, use two consecutive greater than signs (> ). For example, you can use the following command to append the directory list generated by the Dir command to the dirlist.txt file:
Dir> dirlist.txt
To append the output of the netstat command to the end of tcpinfo.txt, type:
Netstat> tcpinfo.txt
Use the pipeline operator (|)
The pipeline operator (|) can extract the output of a command (stdout by default) and direct it to the input of another command (stdin by default. For example, you can use the following command to classify directories:
Dir | sort
In this example, two commands will be started at the same time, but the sort command will be paused until it receives the output of the Dir command. The sort command uses the output of the Dir command as the input, and then sends the output to handle 1 (stdout ).
Merge commands with redirection Operators
You can create custom commands by combining filter commands with other commands and file names. For example, you can use the following command to store the file name containing the "log" string:
DIR/B | find "log" loglist.txt
The output of the Dir command is sent using the find filter command. The file name that contains the string "log" is stored in the loglist.txt file as the file name list (for example, netshconfig. Log, logdat. SVD, and mylog. BAT.
To use multiple filters in the same command, use pipelines (|) to separate filters. For example, the following command searches for each directory on the C drive to find the file name containing the "log" string, and displays a screen at each time in the Command Prompt window:
Dir c: \/S/B | find "log" | more
Pipeline (|) can be used to redirect cmd.exe to send the Dir command output through the find filter command. The find command only selects the file name that contains the string "log. The more command displays the file name selected by the find command (a screen is displayed each time in the Command Prompt window ).