Redirection & nul
Every command started by CMD has three standard streams. Normally they are all connected to the console window, but redirection lets you send a command's output into a file, read its input from a file, or throw output away entirely with the special device nul.
| Stream | Number | Default | Used for |
|---|---|---|---|
| standard input (stdin) | 0 | keyboard | data a command reads |
| standard output (stdout) | 1 | console | normal results |
| standard error (stderr) | 2 | console | error messages and warnings |
> and >>: output to a file
> creates the file (or overwrites it if it exists) and writes the output into it. >> appends to the end of the file, creating it if needed:
@echo off
mkdir demo & cd demo
echo first run> status.txt
echo second run> status.txt
type status.txt
echo --- log:
echo started>> app.log
echo working>> app.log
echo finished>> app.log
type app.logsecond run
--- log:
started
working
finished
The redirection can stand anywhere on the line: > status.txt echo ok and echo ok > status.txt do the same thing, except that the second also writes the space before > into the file.
The digit trap
A digit directly before > is read as a stream number. So echo 1> file.txt means "redirect stream 1 of echo (with no text)", and the file receives ECHO is off. instead of 1. It bites people who write numbers into files:
@echo off
mkdir demo & cd demo
set count=3
echo --- the trap:
echo Total: %count%> count.txt
echo --- count.txt contains:
type count.txt
echo --- the fix:
> count.txt echo Total: %count%
type count.txt--- the trap:
Total:
--- count.txt contains:
--- the fix:
Total: 3
After expansion the first line reads echo Total: 3> count.txt. CMD took 3> as "redirect stream 3", so echo printed Total: on the screen and count.txt stayed empty. Putting the redirection first (> file echo text) is the safe habit whenever the text may end in a digit. The same works for appending: >> file echo text.
Redirecting a whole block
A parenthesized block can be redirected once, which is faster and tidier than redirecting every line:
@echo off
mkdir demo & cd demo
(
echo [report]
echo user=%USERNAME%
echo host=%COMPUTERNAME%
) > report.ini
type report.ini[report]
user=learner
host=TAD
2>: error messages
Error messages are written to stream 2, so > alone does not capture them. Here is what that looks like on Windows with a file that doesn't exist:
C:\Users\ada\demo>dir /b notes.txt missing.txt > out.txt
File Not Found
C:\Users\ada\demo>type out.txt
notes.txt
C:\Users\ada\demo>dir /b notes.txt missing.txt > out.txt 2> err.txt
C:\Users\ada\demo>type err.txt
File Not FoundA batch file can write to stderr itself with >&2, which is how well-behaved scripts report errors. This example uses a small subroutine that writes one line to each stream:
@echo off
mkdir demo & cd demo
call :work > out.txt 2> err.txt
echo --- out.txt:
type out.txt
echo --- err.txt:
type err.txt
exit /b
:work
echo 42 records processed
>&2 echo warning: 3 records skipped
exit /b--- out.txt:
42 records processed
--- err.txt:
warning: 3 records skipped
2>&1: both streams to the same place
2>&1 means "send stream 2 wherever stream 1 is going". Redirections are applied left to right, so the order matters: first move stream 1, then join stream 2 to it.
@echo off
mkdir demo & cd demo
call :work > all.txt 2>&1
echo --- all.txt:
type all.txt
exit /b
:work
echo step 1 ok
>&2 echo step 2 failed
echo step 3 ok
exit /b--- all.txt:
step 1 ok
step 2 failed
step 3 ok
With the opposite order, 2>&1 > all.txt, stream 2 is joined to the console (where stream 1 still pointed at that moment), so the errors would still appear on screen.
| Syntax | Meaning |
|---|---|
cmd > file |
stdout to file (overwrite) |
cmd >> file |
stdout to file (append) |
cmd 2> file |
stderr to file |
cmd > file 2>&1 |
stdout and stderr to the same file |
cmd 2>nul |
discard errors |
cmd >nul 2>&1 |
discard everything |
cmd < file |
stdin from file |
>&2 echo text |
write a message to stderr |
nul: the black hole
nul is a special device name that exists in every folder: anything written to it disappears, and reading from it returns nothing. Scripts use it all the time to silence commands whose output they don't need, while still checking whether they succeeded:
@echo off
mkdir demo & cd demo
echo secret> data.txt
copy data.txt backup.txt >nul && echo Backup created
mkdir logs >nul 2>&1
mkdir logs >nul 2>&1 || echo logs already exists (message hidden)
type nul > empty.txt
for %%f in (empty.txt) do echo empty.txt has %%~zf bytesBackup created
logs already exists (message hidden)
empty.txt has 0 bytes
Note: Write
nul, notnullor/dev/null.> nullcreates an ordinary file callednull. Becausenulis a reserved device name, you also can't create a file callednul.txt,con,prn,aux,com1orlpt1on Windows.
<: input from a file
< connects a file to a command's standard input. Commands that read stdin, such as sort, more, find and set /p, then read the file as if it were typed:
@echo off
mkdir demo & cd demo
(
echo pear
echo apple
echo cherry
) > fruit.txt
sort < fruit.txt
set /p first=< fruit.txt
echo The first line is: %first%apple
cherry
pear
The first line is: pear
sort read all three lines from the file; set /p read only the first one.
Learn more
Redirection works the same way in PowerShell and Bash, with small differences ($null in PowerShell, /dev/null in Bash). Compare them in Redirection, or continue with Pipes.
Test yourself
Which command appends a line to log.txt instead of overwriting it?
- echo done > log.txt
- echo done >> log.txt
- echo done 2> log.txt
- echo done < log.txt
How do you hide both the output and the error messages of a command?
- cmd > nul
- cmd 2> nul
- cmd > nul 2>&1
- cmd 2>&1 > nul
In a batch file with echo off, what does echo 1> count.txt write into count.txt?
- 1
- 1 followed by a space
- The 'ECHO is off.' message, because 1> redirects stream 1
- Nothing; it is a syntax error