What is /dev/null and How to Use It

Linux is an interesting operating system that hosts some virtual devices for numerous purposes. As far as programs running in the system are concerned, these virtual devices act as if they are real files. Tools can request and feed data from these sources. The data is generated by the OS instead of reading them from a disk.

One such example is /dev/null. It’s a special file that’s present in every single Linux system. However, unlike most other virtual files, instead of reading, it’s used to write. Whatever you write to /dev/null will be discarded, forgotten into the void. It’s known as the null device in a UNIX system.

Why would you want to discard something into the void? Let’s check out what /dev/null is and its usage.


Before diving deep into the usage of /dev/null, we have to have a clear grasp of the stdout and stderr data stream. Check out this in-depth guide on stdin, stderr, and stdout.

Let’s have a quick refresh. Whenever any command-line utility is run, it generates two outputs. The output goes to stdout and the error (if generated) goes to stderr. By default, both these data streams are associated with the terminal.

For example, the following command will print out the string within the double quotation mark. Here, the output is stored in stdout.

$ echo “Hello World”

The next command will show us the exit status of the previously-run command.

$ echo $?

As the previous command ran successfully, the exit status is 0. Otherwise, the exit status will be different. What happens when you try to run an invalid command?

$ adfadsf
$ echo $?

Now, we need to know about the file descriptor. In the UNIX ecosystem, these are integer values assigned to a file. Both stdout (file descriptor = 1) and stderr (file descriptor = 2) have a specific file descriptor. Using the file descriptor (1 and 2 in this situation), we can redirect the stdout and stderr to other files.

For starter, the following example will redirect the stdout of the echo command to a text file. Here, we didn’t specify the file descriptor. If not specified, bash will use stdout by default.

$ echo “Hello World” > log.txt

The following command will redirect the stderr to a text file.

$ asdfadsa 2> error.txt

Using /dev/null

Redirecting output to /dev/null

Now, we’re ready to learn how to use /dev/null. First, let’s check out how to filter normal output and error. In the following command, grep will try to search for a string (hello, in this case) in the “/sys” directory.

$ grep -r hello /sys/

However, it will generate a lot of error as without root privilege, grep can’t access a number of files. In such a case, it’ll result in “Permission denied” errors. Now, using the redirection, we can get a clearer output.

$ grep -r hello /sys/ 2> /dev/null

The output looks much better, right? Nothing! In this case, grep doesn’t have access to a lot of files and those that are accessible doesn’t have the string “hello”.

In the following example, we’ll be pinging Google.

$ ping google.com

However, we don’t want to see all those successful ping results. Instead, we only want to focus on the errors when ping couldn’t reach Google. How do we do that?

$ ping google.com 1> /dev/null

Here, the contents of stdout are dumped to /dev/null, leaving only the errors.

Redirect all output to /dev/null

In certain situations, the output may not be useful at all. Using redirection, we can dump all the output into the void.

$ grep -r hello /sys/ > /dev/null 2>&1

Let’s break this command a little bit. First, we’re dumping all the stdout to /dev/null. Then, in the second part, we’re telling bash to send stderr to stdout. In this example, there’s nothing to output. However, if you’re confused, you can always check if the command ran successfully.

$ echo $?

The value is 2 because the command generated a lot of errors.

If you tend to forget the file descriptor of stdout and stderr, the following command will do just fine. It’s a more generalized format of the previous command. Both stdout and stderr will be redirected to /dev/null.

$ grep -r hello /sys/ &> /dev/null

Other examples

This is an interesting one. Remember the dd tool? It’s a powerful tool for converting and copying files. Learn more about dd. Using dd, we can test the sequential read speed of your disk. Of course, it’s not an accurate measurement. However, for a quick test, it’s pretty useful.

$ dd if=<big_file> of=/dev/null status=progress bs=1M iflag=direct

Here, I’ve used the Ubuntu 18.04.4 ISO as the big file.

Similarly, you can also test out the download speed of your internet connection.

$ wget -O /dev/null <big_file_link>

Final thoughts

Hopefully, you have a clear understanding of what this /dev/null file is. It’s a special device that, if written to it, discards and if read from, reads null. The true potential of this interesting feature is in interesting bash scripts.

Are you interested in bash scripting? Check out the beginner’s guide to bash scripting.


About the author

Sidratul Muntaha

Sidratul Muntaha

Student of CSE. I love Linux and playing with tech and gadgets. I use both Ubuntu and Linux Mint.