I/O Redirection and Pipes
Normally, any output from a console program appears in the Command Prompt window, but you can redirect it into a file using the > character. For example, the command
tasklist >tasks.txt
generates the same listing as the previous example but stores it in a file named tasks.txt. Command-line programs send their output to what is called the standard output stream. By default, anything the program sends to the standard output is printed in the Command Prompt window. Figure below shows how this looks. The first tasklist command in the figure sends its output to the Command Prompt window. The second tasklist command has its output redirected into a file.
Some programs read input through a standard input stream. By default, this is connected to your keyboard, and whatever you type is read by the program. For example, the sort command reads lines of text from the standard input, sorts them alphabetically, and writes the results to the standard output. If you type the following lines at the command prompt,
sort c b a Ctrl+Z
sort spits out the lines in order: a, b, c. (Note that Ctrl+Z on a line by itself indicates the end of input.) You can redirect the standard input with the < character. For example, the command
sort <somefile.txt
tells sort to read input from the file somefile.txt.You can use both input and output redirection at the same time.The command
sort <somefile.txt >sortedfile.txt
rearranges the contents of somefile.txt and creates a new file named
sortedfile.txt.
You can also specify that the output isn't to replace an existing file but rather should simply be stuck onto the end (that is, appended ) with the characters >>, as in this example:
dir /b c:\ >listing.txt
dir /b d:\ >>listing.txt
The first command creates the file listing.txt, and the second appends its output onto the end of listing.txt. (If the file listing.txt doesn't already exist, don't worry: >> creates it.)
Finally, you can hook the output of one program directly to the input of another by using the vertical bar symbol (|), usually found above the backslash (\) on a keyboard. For example, the find command reads lines from the input and passes only lines containing a designated string.The command
tasklist | find "winword"
has tasklist send its list of all programs through find which types out only the line or lines containing "winword." Finally,
tasklist | find "winword" >tasks.txt
connects tasklist to find, and find to a file.
Input and output redirection let you connect programs and files as if you are making plumbing connections.The | symbol is often called a pipe for this reason, and programs such as find are often called filters.
Note One handy filter to know about is more, a program that passes whatever it's given as input to its output. What makes more useful is that it pauses after printing a screen full of text. more lets you view long listings that would otherwise scroll off the screen too quickly to read. For example, the command
tasklist | more
helps you see the entire list of programs. When you see the prompt -- More -- at the bottom of the screen, press the spacebar to view the next screen full.
The standard error is another output stream available to console programs. By default, if a program writes information to the standard error stream, the text appears in the Command Prompt window. Programs usually use this to display important error messages that they want you to see, even if the standard output is redirected to a file or pipe.You can redirect standard error too, though, if you want to capture the error messages in a file.
If you've worked with DOS, Linux, or Unix, this is probably already familiar to you. However, there are several input redirection variations you might not be familiar with. Table below lists all the redirection instructions that CMD recognizes.
Input and Output RedirectionRedirection Option | Action |
---|---|
<file | Reads standard input from file. |
>file | Writes standard output to file. |
>>file | Appends standard output to file. |
1>file | Writes standard output to file.* |
1>>file | Appends standard output to file. |
2>file | Writes standard error to file. |
2>>file | Appends standard error to file. |
2>&1 | Directs standard error through the same stream as standard output. Both can then be redirected to a file or piped to another program. |
nextcommand | Sends output to the input of nextcommand. |
* The number 1 refers to the standard output stream and the number 2 to the standard error stream.
Two special forms of output redirection are output to the NUL device and output to a printer.Windows recognizes the special filename nul in any folder on any drive and treats it as a "black hole" file. If you issue a command and redirect its output to nul, the output is discarded.This type of direction is useful in batch files when you want a command to do something, but don't want or need the user to see any error messages it might print. For example,
net use f: /del >nul 2>nul
runs the net use command and makes sure that no output appears to the user.
Special filenames LPT1, LPT2, and so on, represent your default printer and your local printer connected to ports LPT1, LPT2, and so on.You can direct the output of a command to the printer using redirection to these names.You can also direct output to network-connected printers by mapping these devices to network printers using the net use command. Special name PRN is the same as LPT1.
In practical day-to-day use, standard output redirection lets you capture the output of a console program into a file, where you can edit or incorporate it into your documents. Standard error redirection is handy when you need to gather hardcopy evidence from a program that is printing error messages. Input redirection is used most often in a batch file setting, where you want to have a command run automatically from input prepared ahead of time.
Here are some examples:
cmd /? | more
This has the CMD command-line shell print its built-in help information, and it passes the text through more so it can be read a page at a time.
cmd /? > cmd.info
notepad cmd.info
This has CMD print its help information again, but this time, the text is stored in a file, and you view the file with Notepad. (Window-based programs can be useful on occasion.)
tasklist | sort /+60 | more
This has tasklist list all running programs, and it pipes the listing through sort, which sorts the lines starting with column 60.This produces a listing ordered by the amount of memory used by each running program.The sorted output is piped through more so it can be viewed a page at a time.
date /f >ping.txt
ping www.companyabc.com 2>&1 >>ping.txt
This checks whether the site www.mycompany.com can be reached through the Internet.The results, including any errors, are stored in a file named ping.txt.You could then view this file or analyze it with a program.
In this tutorial:
- The CMD Command-Line
- CMD Versus COMMAND
- Running CMD
- Opening a Command Prompt Window with Administrator Privileges
- CMD Options
- Disabling Command Extensions
- Command-Line Processing
- Console Program Input and Output
- Using the Console Window
- I/O Redirection and Pipes
- Copy and Paste in Command Prompt Windows
- Command Editing and the History List
- Name Completion
- Enabling Directory Name Completion
- Multiple Commands on One Line
- Grouping Commands with Parentheses
- Arguments, Commas, and Quotes
- Escaping Special Characters
- Configuring the CMD Program
- The Search Path
- Changing the Path
- Predefined and Virtual Environment Variables
- Setting Default Environment Variables
- Built-in Commands
- Extended Commands
- Listing Files with the Dir Command
- Paginating Long Listings
- Printing Directory Listings
- Sorting Listings
- Locating Alternate File Streams
- Setting Variables with the Set Command
- Conditional Processing with the if Command
- Scanning for Files with the for Command
- Using the for Command's Variable
- Processing Directories
- Numerical for Loop
- Getting More Information