C.W.K.
Stream
Lesson 04 of 10 · published

/dev/null — The Black Hole

~8 min · dev-null, silence

Level 0Window Tourist
0 XP0/95 lessons0/14 achievements
0/100 XP to next level100 XP to go0% complete

The bit bucket

/dev/null is a special file that throws away everything written to it and returns 0 bytes when read. It is the canonical way to silence noise in Unix.

Common uses

  • cmd 2>/dev/null — kill error spam.
  • cmd >/dev/null 2>&1 — kill all output.
  • cmd </dev/null — feed an empty stdin (useful for background jobs that read).
  • : > file or cat /dev/null > file — truncate a file to zero.

The truthy version: /dev/zero

/dev/zero reads as an infinite stream of null bytes. dd if=/dev/zero of=blob bs=1M count=10 creates a 10 MB file of zeros — useful for filesystem tests, swap files, or generating test data. Sibling: /dev/random (cryptographic randomness, slow) and /dev/urandom (also random, fast, fine for almost everything).

Don't accidentally redirect input

cmd > /dev/null hides output. cmd < /dev/null feeds empty stdin. Mix them up and your script silently does nothing because you fed it an empty input stream.

Code

Silence in three flavors·bash
# Just stderr
find / -name foo 2>/dev/null
# Both streams
noisy_cmd >/dev/null 2>&1
# Bash 4+ shorthand
noisy_cmd &>/dev/null

External links

Exercise

Run find / -name passwd 2>/dev/null — see only matches, no permission spam. Then dd if=/dev/zero of=/tmp/10mb bs=1M count=10 and check the file size. Finally clear it with : > /tmp/10mb.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.