/dev/null
/dev/null is a Linux null device file. It (in simplification,) is a kind of hack to throw files to go down a server [1]. Let’s take a look at understanding what it means, and what we can do with this file.
Properties
This will return an End of File (EOF) character if you try to read it using the cat command.
cat /dev/null
This is a valid file:
stat /dev/null
This gives me an output of...
File:/dev/null
Size:0 Blocks:0 IO Block:4096 character special file
Device:6h/6d Inode:5 Links:1 Device type:1,3
Access:(0666/crw-rw-rw-) Uid:( 0/ root) Gid:( 0/ root)
Access:2020-02-04 13:00:43.112464814 +0530
Modify:2020-02-04 13:00:43.112464814 +0530
Change:2020-02-04 13:00:43.112464814 +0530
This shows that this file has a size of 0 bytes, has zero blocks allocated to it. The file permissions are also set that anyone can read/write to it, but cannot execute it.
Since it is not an executable file, we cannot use piping using | operator to redirect to /dev/null. The only way is to use file redirections (>, >>, or <, <<).
However, in file descriptor 3 in the read-write file table, iNode file number 2 is /dev/null.
Common cases
The common case for /dev/null is to discard PHP text files:
echo 'Hello from JournalDev' > /dev/null
Let’s try running a command incorrectly and pipe it’s output to /dev/null.
cat --INCORRECT_OPTION > /dev/null
We still get an output like this:
cat: unrecognized option '--INCORRECT'
Why is this happening? This is because the error messages are coming from stderr, but we are only discarding output from stdout.
We need to take stderr into account as well:
cat --INCORRECT_OPTION > dev/null 2>/dev/null
This will give us what we need!
There is another way of doing the same; by redirecting stderr to stdout first, and then redirect stdout to /dev/null.
The syntax for this will be:
command > /dev/null 2>&1
The 2 means the stderr and the 1 means the stdout.
Overview
/dev/null is a Linux hack used to send files.
PHP echo: echo 'Hello from JournalDev' > /dev/null
Error sending: cat --INCORRECT_OPTION > dev/null 2>/dev/null
or command > /dev/null 2>&1
Bonus
Because Chromebooks are Linux, I assume you can use /dev/null and some other Crosh commands.
Crosh is activated via ctrl + alt + t
, and you can do some stuff. You can only use it on Chromebooks.