/dev/zero

1

/dev/zero is a special file in Unix-like operating systems that provides as many null characters (ASCII NUL, 0x00) as are read from it. One of the typical uses is to provide a character stream for initializing data storage.

Function

Read operations from /dev/zero return as many null characters (0x00) as requested in the read operation. Unlike /dev/null, /dev/zero may be used as a source, not only as a sink for data. All write operations to /dev/zero succeed with no other effects. However, /dev/null is more commonly used for this purpose. When /dev/zero is memory-mapped, e.g., with mmap, to the virtual address space, it is equivalent to using anonymous memory; i.e. memory not connected to any file.

History

/dev/zero was introduced in 1988 by SunOS-4.0 in order to allow a mappable BSS segment for shared libraries using anonymous memory. HP-UX 8.x introduced the MAP_ANONYMOUS flag for mmap, which maps anonymous memory directly without a need to open /dev/zero. Since the late 1990s, MAP_ANONYMOUS or MAP_ANON are supported by most UNIX versions, removing the original purpose of /dev/zero.

Examples

Erasing a file system partition or drive: cp /dev/zero /dev/<destination drive or partition> (Note that this does not perform a secure erasure, may not destroy the data at all, and may take significantly more time than required – for this purpose, domain-specific tooling like blkdiscard may be preferred for devices that support TRIM.) Creating a 1 MiB file, called foobar, filled with null characters: head -c $(( 1024 * 1024 )) /dev/zero > foobar Instead of creating a file really filled with only zero bytes, many file systems also support the creation of sparse files, which return zeros upon reading but use less actual space. The classic way of doing this (without the domain-specific truncate utility) would be, to create a 1 GiB file: dd bs=1 seek=$(( 1024 * 1024 * 1024 - 1 )) count=1 < /dev/zero > foobar which seeks to position seek·bs=1GiB−1 in the output and copies count·bs=1 byte from /dev/zero, thus making the file contain only one data block.

This article is derived from Wikipedia and licensed under CC BY-SA 4.0. View the original article.

Wikipedia® is a registered trademark of the Wikimedia Foundation, Inc.
Bliptext is not affiliated with or endorsed by Wikipedia or the Wikimedia Foundation.

Edit article