Skip to content

Latest commit

ย 

History

History
78 lines (50 loc) ยท 1.61 KB

File metadata and controls

78 lines (50 loc) ยท 1.61 KB

Nice one ๐Ÿ‘ โ€” /dev/zero is a special file in Linux/Unix.


๐Ÿ”น What is /dev/zero?

  • Itโ€™s a character device file that produces an infinite stream of null (zero) bytes (\0).
  • You can read from it endlessly โ€” it never ends.
  • Itโ€™s often used to create empty files, memory buffers, or to overwrite data.

๐Ÿ”น Examples

1. Create a file of a fixed size (filled with zeros)

# 1 MB file filled with zeros
dd if=/dev/zero of=zero_file.bin bs=1M count=1
  • if=/dev/zero โ†’ input file is infinite zeros
  • of=zero_file.bin โ†’ output file
  • bs=1M count=1 โ†’ block size 1 MB, only 1 block

Result โ†’ zero_file.bin is exactly 1 MB, all zeros.


2. Allocate a swap file

dd if=/dev/zero of=/swapfile bs=1M count=1024
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
  • Creates a 1 GB swap file (1024 MB).

3. Fill a disk (dangerous if used wrongly)

dd if=/dev/zero of=bigfile bs=1M
  • Writes zeros until the disk is full.
  • Useful for testing disk write speed (but be careful).

4. Generate padding in scripts

head -c 10 /dev/zero | tr '\0' '#'

Output:

##########
  • Reads 10 null bytes and converts them into #.
  • Used for progress bars, padding, etc.

๐Ÿ”น Difference from /dev/null

  • /dev/zero โ†’ infinite zero bytes as input.
  • /dev/null โ†’ nothing as input (EOF immediately), and discards all output written to it (black hole).

๐Ÿ‘‰ Do you want me to also explain /dev/random and /dev/urandom along with /dev/zero (since theyโ€™re often compared in scripting)?