Note: Under DOS & Win9x/ME some of the simple STDOUT redirection and
pipes work, but none of the advanced STDERR or multiple command methods
are supported.
IO Redirection in Windows NT, 2000, XP, UNIX (sh, bash and variants)
File Descriptors
FD
Description
1
STDOUT
2
STDERR
3+
Additional files as opened by the process
Redirection
Command
Description
cmd1 | cmd1
Pipe STDOUT of cmd1 into STDIN of cmd2
\> file
Direct STDOUT to file, overwriting existing contents
\>\> file
Direct STDOUT to file, appending to existing contents
`>
file`
2\> file
Direct STDERR to file, overwriting existing contents
2\>\> file
Direct STDERR to file, appending to existing contents
\< file
Get STDIN from file
2\>&1
Direct STDERR to the same place as STDOUT
\>& file
Direct both STDOUT and STDERR to file
2\>&
Duplicate STDOUT to STDERR
echo 'foo' \>&2
Send output to STDERR instead of STDOUT
Notes:
Numbered file descriptions above may be used arbitrarily.
noclobber is a UNIX setting that prevents overwriting (clobbering)
existing files by redirection.
UNIX /dev/null is equivalent to Windows NUL. Windows NUL is not case
sensitive.
^ is the meta-character escape in DOS/Windows, so it may sometimes
be necessary to use ^| (e.g. when using egrep in a batch file). You
may use ^^ for a literal ^.
Examples:
Command
Description
dir c:\*.* > myls.txt
Redirect output of ls into myls.txt, overwriting or creating myls.txt if necessary
dir c:\winnt\*.* >> myls.txt
Append more output of ls into myls.txt
noisy_cmd > NUL
Make STDOUT output from noisy_cmd go away
noisy_cmd 2> NUL
Make STDERR output from noisy_cmd go away
noisy_cmd > NUL 2>&1
Make ALL output from noisy_cmd go away
noisy_cmd 2> NUL 1>&2
Make ALL output from noisy_cmd go away
noisy_cmd | more
Pipe noisy_cmd STDOUT into more (or less or whatever)
noisy_cmd 2>&1 | more
Pipe noisy_cmd STDOUT and STDERR into more (this is great for those “net” commands that scroll off the screen when you try to get help)
echo some message 1>&2
Use the echo command to send output to STDERR (it usually goes to STDOUT).
Running Multiple Commands in Windows NT, 2000, UNIX (sh, bash and variants)
Use parentheses to nest as needed.
Command
Description
cmd1 & cmd2
Run cmd1, then run cmd2
cmd1 ; cmd2
Run cmd1, then run cmd2 (UNIX only)
cmd1 && cmd2
Run cmd1. If it finishes successfully then run cmd2