Linux Command Reference ยท Special Files and Directories
The /dev/null device
Reference for /dev/null: discard output with > /dev/null, silence errors with 2>&1, use it as empty input, and test commands by exit code without printing anything.
/dev/null is a special file that swallows everything written to it and returns end-of-file immediately when read. Writes always succeed and the data vanishes; reads produce nothing, as if the file were empty. Shell users call it the "bit bucket".
You use it to hide output you do not care about, to run commands only for their exit status, and to give a program empty input so it does not wait for the keyboard.
command > /dev/null # discard stdout command 2> /dev/null # discard stderr command > /dev/null 2>&1 # discard both command < /dev/null # empty input
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
| Use | Meaning |
|---|---|
> /dev/null |
Redirect standard output (file descriptor 1) into the void. |
2> /dev/null |
Redirect standard error (descriptor 2). |
&> /dev/null |
Bash shortcut for both streams. The portable form is > /dev/null 2>&1. |
< /dev/null |
Give a command empty standard input. |
cat /dev/null > file |
Empty a file (same as : > file or truncate -s 0). |
Order matters: cmd 2>&1 > /dev/null sends errors to the terminal (stderr copies the old stdout first). Write > /dev/null 2>&1 to silence both.
Usage notes
- Silencing everything hides problems. Redirecting stderr throws away the very message that explains a failure. Prefer to hide only the stream you really do not need.
- Check success quietly:
grep -qandcommand -valready avoid output; add2>/dev/nullfor permission errors. - Background jobs:
nohup cmd > /dev/null 2>&1 &stops a daemon-like job from keeping the terminal busy, and< /dev/nullprevents it from reading input. - It is a real device file (character device 1:3), mode
crw-rw-rw-, so anyone can use it. Never replace it with a regular file: some programs would then fill the disk, and tools that check for it break. - Cost: writes cost almost nothing, since the kernel just drops the bytes. It is the fastest sink for benchmarking output-heavy commands.
- Not the same as
/dev/zero. Reading/dev/nullgives end-of-file; reading/dev/zerogives endless zero bytes.