What is /dev/null in Linux?

Linux TLDR
Last Updated:
Reading time: 3 minutes

The “/dev/null” file is a special file that can be found in all Linux systems. They are also referred to as “null device files”, “void”, and sometimes “a black hole of Linux”.

There is not any specific role for this file; however, if you redirect anything to it, it will be discarded or forgotten in limbo without any trace.

But before you understand them deeply, you must also be aware of the concept of stdin, stdout, and stderr streams in Linux.

Tutorial Details

Description/dev/null or null device file
Difficulty LevelModerate
Root or Sudo PrivilegesNo
OS CompatibilityUbuntu, Manjaro, Fedora, etc.
Prerequisitescat, file, stat, echo
Internet RequiredNo

/dev/null File Information

If you read this file using the cat command, it will return the EOF (end of file) characters (nothing).

$ cat /dev/null

Output:

reading dev null file in linux

However, if you check the type of this file using the file command, you will get the following output.

$ file /dev/null

Output:

reading dev null file in linux using the file command

But if you check the file information using the stat command, you will get some more juicy information.

$ stat /dev/null 

Output:

reading dev null file using the stat command

From the above output, it’s clear that the size of this file is “0” bytes. Apart from that, it has zero blocks allocated to it.

But the important information is in the “Access section”, where you will learn that it has read and write permissions except executable.

It means that you cannot use the “|” sign to pipe data from one command to the “/dev/null” file.

But it has read and write permission, which means you can use file redirection signs like (“>“, “>>“, “<“, or “<<“) to redirect anything to the “/dev/null” file.

Redirecting Anything to /dev/null in Linux

Like I told you earlier, if you redirect anything to “/dev/null“, it will suck it up like a vacuum.

For that, I’ll show you an example where I will redirect the echo command (stdout stream) to the “/dev/null” file.

$ echo "Hello, folks!" >> /dev/null

Output:

redirecting stdout stream data to dev null file

The above command will not give you an output because it went into the black hole of the “/dev/null” file.

What if I try to redirect the stderr stream data to the “/dev/null” file? Let’s learn it by doing.

$ cat FILE_NOT_EXISTS > /dev/null

Output:

redirecting stderr stream data to dev null file

As you see, in the case of the stdout stream, we did not get any output because the data was sucked by “/dev/null“.

But in the case of the stderr stream, the data is printed in the terminal as an output, which we did not expect.

It is due to the default nature of the file redirection sign, which by default only redirects the stdout data.

However, you can use the stderr stream(2) unique value to redirect the data to “/dev/null“, which will be sucked.

$ cat FILE_NOT_EXISTS > /dev/null 2>/dev/null

Output:

redirecting stderr stream data to dev null file without printing error

As you can see, in this case we did not get any output from the stdout(1) and stderr(2) stream data because both data went to the “/dev/null” file.

Now that you understand them well, we can play with the data by redirecting from one stream position to another.

For example, in the following command, I will first redirect the cat command (stdout(1)) data to the “/dev/null” file and then redirect the stderr(2) to stdout(1) by using the “&1“, which will mention to the shell that the destination file is a file descriptor and not a file name.

$ cat FILE_NOT_EXISTS > /dev/null 2>&1

Output:

redirecting stdout and stderr stream data to dev null file

If you refuse to add “&” at the beginning of 1“, it will redirect the output to “1” as a file.

I hope you understand the concept of a “/dev/null” file and its uses.

If you have any doubts or questions, feel free to ask them in the comment section.

Join The Conversation

Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.