What Is /dev/null in Linux

By 

Published on

5 min read

Discarding command output by redirecting it to /dev/null in Linux

Read through almost any shell script or crontab and you will eventually find something like > /dev/null 2>&1 tacked onto the end of a command. It looks cryptic, but it is one of the most common patterns in Linux, and it does one simple thing: it throws output away. Understanding /dev/null clears up a whole class of these expressions at once.

This guide explains what /dev/null is and shows how to use it to silence command output and empty files.

What /dev/null Is

/dev/null is a special file known as the null device. It is not a regular file on disk, even though it lives in the filesystem at /dev/null. It is provided by the kernel and behaves in two ways:

  • Anything written to it is discarded. The data simply disappears, and the write reports success.
  • Anything read from it returns end-of-file immediately, so it acts as an empty source.

Because of this, /dev/null is often called the “black hole” of the system. It gives you a place to send output you do not care about, without writing it to a real file or printing it to the screen.

Reading from /dev/null

Reading from /dev/null immediately returns end-of-file. You can see that by counting how many bytes are available from it:

Terminal
wc -c < /dev/null
output
0

The command prints 0 because there is no input to read. This behavior is useful when a command expects input but you want to give it an empty stream. A common case is running a command inside a script that might otherwise pause to read from the keyboard. Pointing its standard input at /dev/null feeds it an immediate end-of-file so it never blocks:

Terminal
ssh user@host "uptime" < /dev/null

Here ssh reads from /dev/null instead of the script’s own input, so it cannot accidentally swallow lines meant for the rest of the script.

Discarding Standard Output

Every command can produce two streams: standard output (stdout) for normal results and standard error (stderr) for error messages. To throw away the normal output of a command while still seeing errors, redirect stdout to /dev/null:

Terminal
find / -name "*.log" > /dev/null

This runs the search but discards the list of matching files. Any “Permission denied” messages still appear on screen, because those go to stderr, which is untouched. The > operator on its own redirects stdout, which has the file descriptor number 1, so > /dev/null is the same as 1> /dev/null.

Discarding Standard Error

To do the opposite and hide only the error messages while keeping the normal output, redirect stderr, which is file descriptor 2:

Terminal
find / -name "*.log" 2> /dev/null

Now the matching files print normally, but the “Permission denied” noise from directories you cannot read is gone. This is a common way to clean up the output of commands that scan large parts of the filesystem.

Discarding Both Streams

When you want a command to run completely silently, send both streams to /dev/null. This is the > /dev/null 2>&1 pattern from the start of the article:

Terminal
command > /dev/null 2>&1

Reading it left to right: > /dev/null sends stdout to the null device, and 2>&1 then sends stderr to wherever stdout currently points, which is /dev/null. The order matters. If you reverse it to 2>&1 > /dev/null, stderr is pointed at the screen first, before stdout is redirected, so error messages still show up.

In the Bash shell there is a shorthand that redirects both streams at once:

Terminal
command &> /dev/null

This is the reason cron jobs so often end this way. A cron job that prints anything tends to generate an email to the owner, so silencing both streams keeps a routine job quiet. For a fuller look at how these streams work, see the guide on redirecting stderr and stdout in Bash .

Emptying a File

Because reading from /dev/null produces no data, you can use it as an empty source to truncate a file. Redirecting it into a file replaces the contents with nothing:

Terminal
cat /dev/null > app.log

The file app.log still exists, but it is now zero bytes. A shorter version uses an empty redirect, which has the same effect:

Terminal
> app.log

This is handy for clearing a log file that has grown too large without deleting and recreating it, which keeps the file’s permissions and any open file handles intact. For more ways to do this safely, see the guide on truncating files in Linux .

Quick Reference

TaskCommand
Discard standard outputcommand > /dev/null
Discard standard errorcommand 2> /dev/null
Discard both streams, POSIX stylecommand > /dev/null 2>&1
Discard both streams in Bashcommand &> /dev/null
Use an empty input streamcommand < /dev/null
Empty a file with shell redirection> file

FAQ

What does > /dev/null 2>&1 mean?
It discards all output from a command. > /dev/null sends standard output to the null device, and 2>&1 redirects standard error to the same place, so neither stream prints anything.

What is the difference between > /dev/null and 2> /dev/null?
> /dev/null discards standard output and leaves error messages visible. 2> /dev/null discards standard error and leaves normal output visible.

Is /dev/null a real file?
It is a special device file provided by the kernel, not a regular file on disk. Writing to it discards data, and reading from it returns end-of-file immediately.

Are /dev/zero and /dev/null the same?
No. /dev/null returns nothing when read, while /dev/zero returns an endless stream of null bytes. Both discard anything written to them, but only /dev/zero produces data on read.

Conclusion

/dev/null is the standard place to send output you do not want, whether that is silencing a noisy command, keeping a cron job quiet, or emptying a log file. Once the > /dev/null 2>&1 pattern makes sense, most of the redirection you see in scripts becomes easy to read.

Tags

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page