[ELI5] /dev/null, /dev/random, /dev/urandom and /dev/zero


I’ve been reading about this files and also I have tried to inspect by myself about this files (file, ll, Stat and some other commands) but I just don’t understand them.

Can someone please explain me what are these files and how can I used them?

In: Technology

Everything in /dev/ is not a real file on a disk, but a sort of pretend file — you can read from them and/or write to them, but their contents or effects live only in software, and are not written down on a disk.

Whenever you read from or write to a file, you don’t directly access the file. Instead, the operating system does that for you.

These three files are special in the sense that when you use them, the OS pretends they are real files when in fact they are “pretend” files.

When you try to write to /dev/null, the OS simply discards your input.

When you read from /dev/zero, the OS just gives you a bunch of zeroes.

And when you read from /dev/random, the OS just gives you a bunch of random numbers.

Null anything you write vanishes – used when you don’t want to record or look at the result of a command.
Zero produces binary zeroes – use it to erase files.
Random produces random binary numbers, attempting to produce high quality output, so reads can be slow.
Urandom produces fast random numbers.
Edit: use them by shell redirection, or with ‘dd’ and ‘cat’. dd count=512 if=/dev/zero of=/filefullofzeroes

If your system has documentation installed, ‘man 4 zero’ and etc should give you much pleasure.

/dev/null is end of file when read and discards data when written. It’s convenient to redirect input for programs that otherwise expect a keyboard, and discard unwanted output like directory errors from “grep <something * 2>/dev/null”.

/dev/random and /dev/urandom are the same driver, which collects entropy (randomness) from activity on the system. /dev/random will delay until it has collected enough random bits for the request, /dev/urandom will “make up” random numbers (using a random number generator) if necessary, so it won’t block. /random provides stronger numbers for things like cryptography, the /urandom is good enough for games and similar programs.

/dev/zero discards written data, and returns just zeros. I’ve used it to rapidly (and only mildly securely) overwrite files, and a source of data to create files of fixed size.