SoFunction
Updated on 2025-03-09

Redirection output of cmd command 2>&1 Detailed explanation

About the redirection output of cmd command 2>&1

mycommand > 2>&1 should be the most classic usage.

The result of the command can be output in the form of "%>", which represents the file descriptor:

1 is the standard output stdout, 2 is the standard error stderr.

The system defaults to % value is 1, which means "1>", and 1> can be abbreviated as >, which means default is >. The default target of stdout is the terminal, and the default target of stderr is also the terminal. We execute: echo text > in batch processing, and we can see echo text 1> on the screen, which is the reason.
Among them & needs to be used directly in conjunction with redirect symbols.

Application example:

1. Output the result to

net stop myservices >>result 2>&1

2. Hide the program output results

net stop myservices >nul 2>nul

Microsoft's article on redirection: Using command redirection operators

The command input and output streams can be redirected from the default location to another location using the redirect operator. The location of the input or output data stream is called a handle.
The following table lists the available handles.

Handle Number code of the handle Description
STDIN 0
Keyboard input

STDOUT 1
Output to the command prompt window

STDERR 2
Error output to command prompt window

UNDEFINED 3-9
Handles are defined separately by the application, and they are unique to each tool.

The numbers 0 to 9 represent the first 10 handles. You can run the program using commands and redirect any of the first 10 handles of the program. To specify the handle to use, type the number for the handle before the redirect operator. If a handle is not defined, the default < redirect input operator is 0, while the default > redirect output operator is 1. After typing the < or > operator, you must specify the read and write location of the data. You can specify a file name or other existing handle.
To specify a redirect to an existing handle, use the same character followed by the handle number (i.e., the & handle number) to be redirected.

For example, the following command can redirect handle 2 (i.e. STDERR) to handle 1 (i.e. STDOUT):

2>&1

The following table lists the operators that can be used to redirect input and output data streams.

Redirection Operator Description
> Write command output to a file or device (such as a printer), rather than a command prompt window or handle.
< Read command input from a file instead of from a keyboard or handle.
>> Add command output to the end of the file without deleting existing information in the file.
>& Writes the output of one handle to the input of another handle.
<& Read input from one handle and write it to another handle output.
| Read the output from one command and write it to the input of another command. Also known as pipe.

By default, command input (i.e., STDIN handle) can be sent from the keyboard, and then command output (i.e., STDOUT handle) is sent to the command prompt window by .

Redirect input (<)

To redirect input to a file or device via the keyboard, use the < operator. For example, to get input from the sort command, type:

sort<

The contents of the sequential list will be displayed in the command prompt window.

The < operator can open a specified file name with read-only access. Therefore, information cannot be written into the file when using this operator. For example, if the program is started with <&2, all attempts to read handle 0 will fail because handle 2 was initially opened in a write-only access mode.

Notice

0 is the default handle for the < redirect input operator.

Redirect output (>)

Almost all commands send output to the command prompt window. Even commands that send output to the drive or printer will display messages and prompts in the command prompt window.

To redirect 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 dir output to , type:

dir>

If it does not exist, the file is created. If present, the information in the file will be replaced with the output of the dir command.

To run the netsh routing dump command and then send the output to , type:

netsh routing dump>c:\

The > operator can open a specified file with write-only access. Therefore, the file cannot be read using this operator. For example, if you start the program with the redirect operator >&0, all attempts to write handle 1 will fail because handle 0 was initially opened as read-only access.

Notice

1 is the default handle for > redirecting the output operator.
Copy handle
Redirect operator & can copy output or input from one specified handle to another specified handle. For example, to send dir output to and error output to , type:

dir>c:\ 2>&1

When copying a handle, you can copy all the characteristics of the original state of the handle. For example, if a handle has a read-only access property, all copies of that handle have a read-only access property. A handle with a read-only access property cannot be copied to another handle with a write-only access property.

Redirect input and copy using the & operator

To use the redirect input operator (<) with the copy operator (&), the specified file must already exist. If the input file exists, the file is opened read-only, and the characters contained in the file are sent as input to this command (as if you entered from the keyboard). If a handle is specified, copy the specified handle to an existing handle on the system.

For example, to open as handle 0 input read (i.e. STDIN), type:

<

To open and send the output to the command prompt window (i.e. STDOUT) after the content is sorted, type:

sort<

To find , and then redirect handle 1 (i.e. STDOUT) and handle 2 (i.e. STDERR) to , type:

findfile > 2<&1

To copy a user-defined handle 3 as handle 0 enter read (i.e. STDIN), type:

<&3

Redirect output and copy using the & operator

If the output is redirected to a file and an existing file name is specified, the file is opened in a write-only manner and the file contents are overwritten. If a handle is specified, copy the file to an existing handle.

To copy a user-defined handle 3 to handle 1, type:

>&3

To redirect all output including handle 2 (i.e. STDERR) from the ipconfig command to handle 1 (i.e. STDOUT), and then redirect the output to , type:

>> 2>&1

Append output using >> Redirect operator

To add output from the command to the end of the file without losing any information already in the file, use two consecutive greater than signs (i.e. >>). For example, you can attach a directory list generated by the dir command to a file using the following command:

dir>>

To append the output of the netstat command to the end of , type:

netstat>>

Use Pipeline Operator (|)

The pipeline operator (|) can extract the output of a command (STDOUT by default) and then direct it to the input of another command (STDIN by default). For example, use the following command to classify directories:

dir | sort

In this example, both commands are initiated at the same time, but the sort command then pauses until it receives the output of the dir command. The sort command uses the output of the dir command as input and then sends the output to handle 1 (i.e. STDOUT).

Merge commands with redirect operators

Custom commands can be created by merging filter commands with additional 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"

The output of the dir command is sent through the find filter command. The file name containing the string "LOG" is stored in the file as a list of file names (for example, , and ).

To use multiple filters in the same command, use a pipeline (|) to separate the filters. For example, the following command will search each directory on the C drive to find the file name containing the "LOG" string, and display one screen at a time in the command prompt window:

dir c:\ /s /b | find "log" | more

Pipeline (|) can be used to direct , so that it sends the dir command output through the find filter command. The find command selects only the file name containing the string "LOG". The more command can display the file name selected by the find command (one screen is displayed at a time in the command prompt window).

This is the article about the detailed explanation of the cmd command redirection output 2>&1. For more related cmd redirection output content, please search for my previous article or continue browsing the related articles below. I hope everyone will support me in the future!