What are /dev/null and /dev/zero, and what are they used for?
/dev/null is a black hole: anything written to it vanishes and reading from it returns nothing. /dev/zero produces an endless stream of zero bytes. They are used to discard output and to create blank files of a given size.
Explain it simply
/dev holds device files, which let you talk to hardware and kernel features as if they were files. Two special ones are not hardware at all.
/dev/null is like a dustbin with no bottom. If a command prints noisy output you do not need, send it there: command > /dev/null 2>&1. Reading from it returns end-of-file immediately, which is a quick way to empty a file: cat /dev/null > app.log.
/dev/zero is a tap that never stops giving zero bytes. With dd it makes a file of exact size, useful for testing disk speed or creating a swap file.
/dev/random and /dev/urandom are the cousins that give random bytes.
Example
ls /nonexistent 2> /dev/null # error message is thrown away
dd if=/dev/zero of=blank.img bs=1M count=10
ls -l blank.img
# -rw-r--r-- 1 user user 10485760 ... blank.imgWhat to say in the interview
"/dev/null discards anything written to it, which I use to silence output or errors. /dev/zero gives an endless stream of zero bytes, handy with dd to create a file of a known size. Both are special device files, not real storage."
Common mistakes
- Writing 2>&1 > /dev/null in the wrong order, so errors still reach the screen.