In technical terms, “/dev/null” is a virtual device file. As far as programs are concerned, these are treated just like real files. Utilities can request data from this kind of source, and the operating system feeds them data. But, instead of reading from disk, the operating system generates this data dynamically. An example of such a file is “/dev/zero.”
In this case, however, you will write to a device file. Whatever you write to “/dev/null” is discarded, forgotten, thrown into the void. To understand why this is useful, you must first have a basic understanding of standard output and standard error in Linux or *nix type operating systems.
stdout and stder
A command-line utility can generate two types of output. Standard output is sent to stdout. Errors are sent to stderr.
By default, stdout and stderr are associated with your terminal window (or console). This means that anything sent to stdout and stderr is normally displayed on your screen. But through shell redirections, you can change this behavior. For example, you can redirect stdout to a file. This way, instead of displaying output on the screen, it will be saved to a file for you to read later – or you can redirect stdout to a physical device, say, a digital LED or LCD display.
A full article about pipes and redirections is available if you want to learn more.
2>you redirect standard error messages. Example:
1>you redirect standard output.
&>you redirect both standard error and standard output.
Use /dev/null to Get Rid of Output You Don’t Need
Since there are two types of output, standard output and standard error, the first use case is to filter out one type or the other. It’s easier to understand through a practical example. Let’s say you’re looking for a string in “/sys” to find files that refer to power settings.
grep -r power /sys/
There will be a lot of files that a regular, non-root user cannot read. This will result in many “Permission denied” errors.
These clutter the output and make it harder to spot the results that you’re looking for. Since “Permission denied” errors are part of stderr, you can redirect them to “/dev/null.”
grep -r power /sys/ 2>/dev/null
As you can see, this is much easier to read.
In other cases, it might be useful to do the reverse: filter out standard output so you can only see errors.
ping google.com 1>/dev/null
The screenshot above shows that, without redirecting, ping displays its normal output when it can reach the destination machine. In the second command, nothing is displayed while the network is online, but as soon as it gets disconnected, only error messages are displayed.
You can redirect both stdout and stderr to two different locations.
ping google.com 1>/dev/null 2>error.log
In this case, stdout messages won’t be displayed at all, and error messages will be saved to the “error.log” file.
Redirect All Output to /dev/null
Sometimes it’s useful to get rid of all output. There are two ways to do this.
grep -r power /sys/ >/dev/null 2>&1
>/dev/null means “send stdout to /dev/null,” and the second part,
2>&1, means send stderr to stdout. In this case you have to refer to stdout as “&1” instead of simply “1.” Writing “2>1” would just redirect stdout to a file named “1.”
What’s important to note here is that the order is important. If you reverse the redirect parameters like this:
grep -r power /sys/ 2>&1 >/dev/null
it won’t work as intended. That’s because as soon as
2>&1 is interpreted, stderr is sent to stdout and displayed on screen. Next, stdout is supressed when sent to “/dev/null.” The final result is that you will see errors on the screen instead of suppressing all output. If you can’t remember the correct order, there’s a simpler redirect that is much easier to type:
grep -r power /sys/ &>/dev/null
In this case,
&>/dev/null is equivalent to saying “redirect both stdout and stderr to this location.”
Other Examples Where It Can Be Useful to Redirect to /dev/null
Say you want to see how fast your disk can read sequential data. The test is not extremely accurate but accurate enough. You can use
dd for this, but dd either outputs to stdout or can be instructed to write to a file. With
of=/dev/null you can tell dd to write to this virtual file. You don’t even have to use shell redirections here.
if= specifies the location of the input file to be read;
of= specifies the name of the output file, where to write.
dd if=debian-disk.qcow2 of=/dev/null status=progress bs=1M iflag=direct
In some scenarios, you may want to see how fast you can download from a server. But you don’t want to write to your disk unnecessarily. Simply enough, don’t write to a regular file, write to “/dev/null.”
wget -O /dev/null http://ftp.halifax.rwth-aachen.de/ubuntu-releases/18.04/ubuntu-18.04.2-desktop-amd64.iso
Hopefully, the examples in this article can inspire you to find your own creative ways to use “/dev/null.”
Know an interesting use-case for this special device file? Leave a comment below and share the knowledge!
Our latest tutorials delivered straight to your inbox