/dev/null: Difference between revisions

From Chaz Wiki
Jump to navigation Jump to search
(Undo revision 1704 by Blotre (talk) because the page is a joke)
Tags: Undo Reverted
(See last edit)
Tags: Blanking Manual revert
 
Line 1: Line 1:
[[/dev/null]] is a Linux null device file. It (in [[Chaz Wiki:Manual of Style|simplification]],) is a kind of hack to throw files to go down a server
<sup>[https://www.digitalocean.com/community/tutorials/dev-null-in-linux <nowiki>[1]</nowiki>]</sup>.
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.


<code>cat /dev/null</code>
This is a ''valid'' file:
<code>stat /dev/null</code>
This gives me an output of...
<code>
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
</code>
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:
<code>echo 'Hello from JournalDev' > /dev/null</code>
Let’s try running a command incorrectly and pipe it’s output to /dev/null.
<br>
<code>cat --INCORRECT_OPTION > /dev/null</code>
We still get an output like this:
<br>
<code>cat: unrecognized option '--INCORRECT'</code>
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:
<br>
<code>cat --INCORRECT_OPTION > dev/null 2>/dev/null</code>
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:
<br>
<code>command > /dev/null 2>&1</code>
The '''2''' means the stderr and the '''1''' means the stdout
=Overview=
[[/dev/null]] is a Linux hack used to send files.
PHP echo: <code>echo 'Hello from JournalDev' > /dev/null</code>
Error sending: <code>cat --INCORRECT_OPTION > dev/null 2>/dev/null</code> or <code>command > /dev/null 2>&1</code>
=Bonus=
Because Chromebooks are Linux, I assume you can use /dev/null and some other Crosh commands.
Crosh is activated via <code>ctrl + alt + t</code>, and you can do some stuff. You can only use it on Chromebooks.

Latest revision as of 23:11, 18 April 2025