/dev/zero: Difference between revisions
removed the link to low-level formatting. →Examples: Zeroing media is not equivalent to low level formatting, as explained in that article. |
Don't use dd to make files. The count=1 bs=1G example is especially egregious because it doesn't actually do what's described (as-written it does buf = malloc(1G); write(1, buf, read(0, buf, 1G)); this may actually write out whatever) |
||
Line 15: | Line 15: | ||
==Examples== |
==Examples== |
||
The [[dd (Unix)|dd]] Unix utility program reads octet streams from a source to a destination, possibly performing data conversions in the process. |
|||
[[Data erasure| |
[[Data erasure|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 on device that support [[Trim (computing)|TRIM]].) |
|||
Creating a 1 [[mebibyte|MiB]] file, called ''[[foobar]]'', filled with null characters:<ref>{{cite book|last1=Optimizing NFS Performance: Tuning and Troubleshooting NFS on HP-UX Systems|first1=Dave|title=Olker|date=2002|publisher=Prentice Hall Professional|isbn=9780130428165|pages=26–7|url=https://books.google.com/books?id=AEV1x84DVkUC&q=%2Fdev%2Fzero&pg=PA27|accessdate=16 July 2014}}</ref> |
|||
Creating a 1 [[mebibyte|MiB]] file, called ''[[foobar]]'', filled with null characters: |
|||
dd if=/dev/zero of=foobar count=1024 {{abbr|bs|block size}}=1024 |
|||
head -c $(( 1024 * 1024 )) /dev/zero > foobar |
|||
Note: The block size value can be given in SI (decimal) values, e.g. in GB, MB, etc. To create a {{val|1|ul=GB}} file one would simply type: |
|||
⚫ | Instead of creating a file really filled with only zero bytes, many file systems also support the creation of [[sparse file]]s, which return zeros upon reading but use less actual space. The classic way of doing this (the domain-specific ''truncate'' utility) would be, to create a 1 [[gibibyte|GiB]] file: |
||
dd if=/dev/zero of=foobar count=1 bs=1GB |
|||
dd {{abbr|bs|block size}}=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. |
|||
==See also== |
==See also== |
Revision as of 17:39, 14 April 2024
/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.[1] One of the typical uses is to provide a character stream for initializing data storage.[2]
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.[3] HP-UX 8.x introduced the MAP_ANONYMOUS flag for mmap(), which maps anonymous memory directly without a need to open /dev/zero.[4] Since the late 1990s, MAP_ANONYMOUS[5] or MAP_ANON are supported by most UNIX versions, removing the original purpose of /dev/zero.[6]
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 on device 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 (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.
See also
References
- ^ Mitchell, Mark; Oldham, Jeffrey; Samuel, Alex (2001), "6.5.2 /dev/zero", Advanced Linux Programming, Sams Publishing, p. 136, ISBN 9780735710436
- ^ Love, Robert (2007), "Mapping /dev/zero", Linux System Programming: Talking Directly to the Kernel and C Library, O'Reilly Media, Inc., pp. 259–260, ISBN 9780596009588
- ^ ""C" run-time program bootstrap from SunOS, contributed to CSRG for inclusion in 4.4BSD". TUHS.
- ^ "HP-UX 8.0.7 install media". 1992-07-22.
- ^ Beal, Chris. "So what the heck is anonymous memory". Oracle Blog. Archived from the original on 2021-04-15. Retrieved 2019-09-09.
- ^ "MAP_ANON description in mmap(2)". NetBSD. Archived from the original on 2019-11-25. Retrieved 2019-09-09.