What is /dev/null in Linux?

If you delve into shell programming, you have come across /dev/null or 2>/dev/null numerous times. At first, this might look like intimidating abstract jargon, but there is a very important (and easy to understand) reason for which it exists. In this article, we will look at the meaning of /dev/null and how it is used in shell programming.

What is /dev/?

On Linux, everything from a driver to a device is accessed as a file. /dev/ is a directory that contains all your physical and virtual devices. For example, /dev/sda might be your primary hard drive and /dev/sdb might be the file for the pen drive you are using right now.

This is how you access devices in Linux. Apart from these physical devices like hard drives, Linux also has virtual devices.

Virtual devices are devices that act like physical devices but in fact, only exist in the software form. Applications can get data from these devices but that data instead of coming from a physical device comes from the operating system itself.

What is /dev/null?

/dev/null is a special virtual device that is used for writing instead of reading. Anything that is written to /dev/null disappears from the OS. For this very reason /dev/null is called the bitbucket.

For example, here I redirected the output of echo in /dev/null. As you can see there is no output. The output “Hello World” is not stored in any file on the system. It is simply gone.

Hello World Dev Null

But why would you ever want to use something like this? Let’s look at the usage of /dev/null.

Usage of /dev/null

The Linux command line is used by system administrators and even casual users for everything. These tasks can be as simple as launching Firefox or as tedious as fixing a systemd issue.

Fixing any sort of issue requires reading a lot of logs whether that be systemd logs, startup logs, server logs etc. The biggest way you will see /dev/null being used is by discarding Standard Output or Standard Error from the whole output.

What are Standard Output and Standard Error?

Whenever you run any command, two streams of output are generated i.e. Standard Output (stdout) and Standard Error (stderr). Standard Output is whatever output that program generates normally. Standard Error contains all the errors generated by the command.

The stdout and stderr streams can be accessed by their file descripters, 1 and 2 respectively.

For example, the following command outputs the standard output in output.txt and standard error in error.txt.

Strderr Stdout

How to use /dev/null with stdout and stderr?

If you want to see only errors or only the standard output message without errors, you can redirect one stream into a random text file as we did in the example above. But it consumes memory and ram and is mostly useless data. That is why we redirect the output stream in /dev/null.

For example, here I am trying to search for any errors in systemd logs i.e. journalctl and ignoring all the other output by redirecting it into /dev/null

Journalctl Dev Null

As you can see, journalctl doesnt return with any errors.

Going back to the grep example in the previous section. What if we just want the messages where grep found a match instead of all of “permission denied” error statements.

Using Dev Null

What does > /dev/null 2>&1 mean?

Sometimes none of the output is useful for the end user. For that, we redirect all the output to /dev/null. There are two ways to do this. The first one and most frequently used is $command >/dev/null 2>&1. Here we redirect stdout to /dev/null which gets destroyed, and after that, we also redirect the stderr to stdout (2>&1). This way none of the output remains.

Redirecting All Output To Null

Here we did not want a lengthy output. We simply wanted to see wheather journalctl returned any errors or not. echo $? returning a 0 means the last command ran without errors. If it returned 2, that would mean the last command ran with errors.

Conclusion

In this article we discussed the meaning and usage of /dev/null. We saw that /dev/null is a void in Linux, which sucks up any input given to it and returns nothing. Mostly, administrators use if for dumping useless data, output in it so that it doesnt hog system memory and processing power. You can even delete useless files by moving them directly to /dev/null/. Have fun and keep exploring!