Home Linux Commands /dev/null 2>&1’ in Linux">What is ‘> /dev/null 2>&1’ in Linux

What is ‘> /dev/null 2>&1’ in Linux

/dev/null is a pseudo-device file in Linux, which is used to discard output coming from programs, especially the ones executed on the command line. This file behaves like a sink, i.e. a target file which can be written, however as soon as any stream of data is written to this file, it is immediately deleted.

This is useful to get rid of the output that is not required by the user. Programs and processes can generate output logs of huge length, and it gets messy at times to analyze the log.

For example, sometimes there are two errors being thrown by a program, but the error logs are lost somewhere in between hundreds of information message logs. In such a case we can dump the information message logs to /dev/null.

In the above example, the error output by a program is called STDERR (Standard Error) in Linux and is represented by the integer ‘2’, while the success and information output is called STDOUT (Standard Output), and is represented by the integer ‘1’.

Now, you might have seen the following command line statement very often, which discards both STDERR and STDOUT to /dev/null.

$ command_name > /dev/null 2>&1

Let us try to understand what this means.

The operator '>' is called a redirection operator. It redirects the output to the file specified after it, which in this case is /dev/null, instead of printing it on the terminal.

Does this mean ALL the output of the command is redirected to /dev/null with the '>' operator? No. When used as shown above, the operator only redirects the STDOUT.

Hence, we have the last part of the command. '2>&1' means: redirect STDERR (2) to STDOUT (1). The reason we have to specify ‘&1’ and not simply ‘1’ is that simply writing ‘1’ will redirect the output to a text file with the name ‘1’. With the ampersand (&) the system recognizes that the 1 is not a filename, but the file descriptor STDOUT.

Thus, we redirect STDOUT to /dev/null and STDERR to STDOUT and in such a way, the complete output of the program(s) is discarded in /dev/null.

For example, executing mv command with and without ‘>/dev/null 2>&1’.

Without redirection:

$ mv tmp.txt test/tmp2.txt /tmp
Move Files Without Redirection
Move Files Without Redirection

With redirection of STDOUT:

$ mv tmp.txt test/tmp2.txt /tmp > /dev/null
Move Files With STDOUT Redirection
Move Files With STDOUT Redirection

With redirection of STDOUT and STDERR:

$ mv tmp.txt test/tmp2.txt /tmp > /dev/null 2>&1
Move Files With STDOUT and STDERR Redirection
Move Files With STDOUT and STDERR Redirection
Conclusion

In this article, we tried to understand what is the meaning of the popularly used command phrase in Linux: '> /dev/null 2>&1'. If you want to learn about ‘/dev/null‘ in more depth, you can do so here.

If you have any questions or comments, let us know in the comments below!

Ravi Saive
I am an Experienced GNU/Linux expert and a full-stack software developer with over a decade in the field of Linux and Open Source technologies. Founder of TecMint.com, LinuxShellTips.com, and Fossmint.com. Over 150+ million people visited my websites.

Each tutorial at UbuntuMint is created by a team of experienced writers so that it meets our high-quality standards.

Was this article helpful? Please add a comment to show your appreciation and support.

1 thought on “What is ‘> /dev/null 2>&1’ in Linux”

Got something to say? Join the discussion.

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published or shared. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.