How to Redirect Output and Error to /dev/null in Linux

Imagine this scenario. You run a Linux command and it has a huge output. You don't want to see its output.

Or, you are using certain Linux commands in your script and you don't want it to show any output or error on the terminal screen.

For such cases, you can take advantage of the output and error redirection and send them to /dev/null.

Send the output to /dev/null:

command 1> /dev/null

Send the error to /dev/null:

command 2> /dev/null

Send both output and error to /dev/null:

command > /dev/null 2>&1 

The /dev/null can be considered a black hole of the Linux file system so whatever you throw there can never see the light again.

Let's take a detailed look here.

Redirect output to /dev/null in Linux

Let me start off with the basics. You type a command in the terminal is your input (let's say I executed sudo apt update).

So I gave input to my system and it will update the directories and it will show me the ongoing processes such as repositories being updated and packages that are outdated now:

In short, it gave the output showing what it did with the command. Now, here is the number designation for each standard data flow:

  • Standard input (stdin) is designated with 0
  • Standard output (stdout) is designated with 1
  • Standard error (stderr) is designated with 2

So now, let me show you how you can redirect standard output to the dev/null:

command 1> /dev/null

For example, I will intentionally use the find command which will show output with an error so as I redirect the output to /dev/null, the error should remain intact:

As you can see, when I used the find command without redirecting the output, it showed output worth 1807 lines.

And when I redirected the output, it showed the errors only as it belongs to the standard error stream.

Redirect errors to /dev/null in Linux

As I mentioned earlier, the error stream is designated with 2. So you just have to make a few changes in the above command and you'd be good to go!

To redirect errors, you will have to use > symbol to redirect the data flow with the number 2 indicating it to redirect data flow on standard error.

command 2> /dev/null

For example, I will use sudo dnf update on Ubuntu so the error is obvious and on the second window, I will demonstrate how you can redirect the error:

Redirect both output and error to /dev/null in Linux

In this section, I will show you how you can redirect both output and the error to /dev/null.

The basic syntax for redirecting output and error to /dev/null is as follows:

command 1> /dev/null 2> /dev/null

Or you can also use the shorter version:

command > /dev/null 2>&1

Which means that send the stderr (2) to the same place as stdout (1). And the stdout (out of the command) is being redirected to /dev/null. Thus, stderr also gets redirected to /dev/null/.

For this example, I will be using the find command to find files in the etc directory in which you'd need sudo privileges to access some sub-directors and when used without sudo, it will throw an error:

Wrapping Up

This was my take on how you can redirect the output and errors to /dev/null. Sometimes, you may even want to redirect the command output to a file and save it for later.

And if you want to learn more about redirecting data flow, I would highly recommend you the detailed guide on redirecting data flow in Linux.