/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.
|Description||/dev/null or null device file|
|Root or Sudo Privileges||No|
|OS Compatibility||Ubuntu, Manjaro, Fedora, etc.|
|Prerequisites||cat, file, stat, echo|
/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
However, if you check the type of this file using the file command, you will get the following output.
$ file /dev/null
But if you check the file information using the stat command, you will get some more juicy information.
$ stat /dev/null
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 “
But it has read and write permission, which means you can use file redirection signs like (“
<“, or “
<<“) to redirect anything to the “
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 “
$ echo "Hello, folks!" >> /dev/null
The above command will not give you an output because it went into the black hole of the “
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
As you see, in the case of the stdout stream, we did not get any output because the data was sucked by “
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
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 “
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
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.