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.
|Description||/dev/null or null device file|
|Root or Sudo Privileges||No|
|Host System and Architecture||Ubuntu 22.10 (x64)|
|OS Compatibility||Ubuntu, Manjaro, Fedora, etc.|
|Prerequisites||cat, file, stat, echo|
|Discussed Tools in this Article|
/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 /dev/null: character special (1/3)
But if you check the file information using the stat command, you will get some more juicy information.
$ stat /dev/null File: /dev/null Size: 0 Blocks: 0 IO Block: 4096 character special file Device: 5h/5d Inode: 5 Links: 1 Device type: 1,3 Access: (0666/crw-rw-rw-) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2022-11-22 10:34:21.940000081 +0530 Modify: 2022-11-22 10:34:21.940000081 +0530 Change: 2022-11-22 13:56:12.071022180 +0530 Birth: -
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
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 cat: FILE_NOT_EXISTS: No such file or directory
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
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
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.