Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

When trying to redirect to /dev/null and /dev/zero, the output it is discarded. It seems both /dev/null and /dev/zero accept and discard all input. So, what is the difference between /dev/null and /dev/zero?

share|improve this question
3  
rm -f /dev/zero ; echo -n 111111111111 > /dev/zero *runs away* – joeytwiddle Jan 10 at 19:23
up vote 20 down vote accepted

Yes, both accept and discard all input, but their output is not the same:

  • /dev/null produces no output.
  • /dev/zero produces a continuous stream of NULL (zero value) bytes.

You can see the difference by executing cat /dev/null and cat /dev/zero.

  • Try cat /dev/null > file and you will find an empty file.

  • Now try cat /dev/zero > file, while watching the size of the file (watch -n 1 du -h file) continuously increase. This is because reading from /dev/zero gives an endless stream of \0 (null) characters.

Use dd to visualize the difference more appropriately:

$ dd if=/dev/null of=file count=10
0+0 records in
0+0 records out
0 bytes (0 B) copied, 0.000276193 s, 0.0 kB/s

$ dd if=/dev/zero of=file count=10
10+0 records in
10+0 records out
5120 bytes (5.1 kB) copied, 0.00090775 s, 5.6 MB/s

/dev/zero is used to create dummy files or swap.

Also visit:

share|improve this answer
    
Also see thecodelesscode.com/case/6 – kojiro Jan 10 at 16:37
1  
Worth noting: mmapping /dev/zero with MAP_PRIVATE is the "portable" way to obtain an anonymous memory mapping (in the absence of extensions like MAP_ANON). – nneonneo Jan 10 at 23:11
1  
Perhaps not obvious to the casual reader is just how /dev/null produces no output: It signals EOF immediately. – Peter A. Schneider Jan 11 at 4:30

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.