Jump to content

/dev/zero: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
Tag: repeating characters
 
(29 intermediate revisions by 20 users not shown)
Line 1: Line 1:
{{Special device files}}
{{Confuse|Null device}}
{{Short description|Special file in Unix-like operating systems}}
{{mono|'''/dev/zero'''}} is a [[special file]] in [[Unix-like]] [[operating system]]s that provides as many [[null character]]s ([[ASCII]] NUL, 0x00) as are read from it.<ref>{{citation|title=Advanced Linux Programming|first1=Mark|last1=Mitchell|first2=Jeffrey|last2=Oldham|first3=Alex|last3=Samuel|publisher=Sams Publishing|year=2001|isbn=9780735710436|url=http://books.google.com/books?id=oRqqAVyXjwAC&pg=PA136|contribution=6.5.2 /dev/zero|page=136}}</ref> One of the typical uses is to provide a character stream for initializing data storage.<ref>{{citation|title=Linux System Programming: Talking Directly to the Kernel and C Library|first=Robert|last=Love|publisher=O'Reilly Media, Inc.|year=2007|isbn=9780596009588|url=http://books.google.com/books?id=k_ocKY0iegsC&pg=PA259|contribution=Mapping /dev/zero|pages=259–260}}</ref>
{{Use dmy dates|date=February 2020|cs1-dates=y}}
{{mono|'''/dev/zero'''}} is a [[special file]] in [[Unix-like]] [[operating system]]s that provides as many [[null character]]s ([[ASCII]] NUL, 0x00) as are read from it.<ref>{{citation|title=Advanced Linux Programming|first1=Mark|last1=Mitchell|first2=Jeffrey|last2=Oldham|first3=Alex|last3=Samuel|publisher=Sams Publishing|year=2001|isbn=9780735710436|url=https://books.google.com/books?id=oRqqAVyXjwAC&pg=PA136|contribution=6.5.2 /dev/zero|page=136}}</ref> One of the typical uses is to provide a character stream for initializing data storage.<ref>{{citation|title=Linux System Programming: Talking Directly to the Kernel and C Library|first=Robert|last=Love|publisher=O'Reilly Media, Inc.|year=2007|isbn=9780596009588|url=https://books.google.com/books?id=k_ocKY0iegsC&pg=PA259|contribution=Mapping /dev/zero|pages=259–260}}</ref>


==Function==
==Function==
Line 10: Line 12:


==History==
==History==
{{mono|/dev/zero}} was introduced in 1988 by SunOS-4.0 in order to allow a mappable BSS segment for shared libraries using anonymous memory.<ref>{{cite web |title="C" run-time program bootstrap from SunOS, contributed to CSRG for inclusion in 4.4BSD |url=https://minnie.tuhs.org/cgi-bin/utree.pl?file=4.4BSD/usr/src/contrib/sun.sharedlib/lib/csu/m68k/crt0.s |website=TUHS}}</ref> HP-UX 8.x introduced the MAP_ANONYMOUS flag for mmap(), which maps anonymous memory directly without a need to open {{mono|/dev/zero}}.<ref>{{cite web |title=HP-UX 8.0.7 install media |date=22 July 1992 |url=https://archive.org/details/hp-ux8.07forhp9000s7xx}}</ref> Since the late 1990s, MAP_ANONYMOUS<ref>{{cite web |last1=Beal |first1=Chris |title=So what the heck is anonymous memory |url=https://blogs.oracle.com/cwb/so-what-the-heck-is-anonymous-memory |website=Oracle Blog |access-date=2019-09-09 |archive-date=2021-04-15 |archive-url=https://web.archive.org/web/20210415103111/https://blogs.oracle.com/cwb/so-what-the-heck-is-anonymous-memory |url-status=dead }}</ref> or MAP_ANON are supported by most UNIX versions, removing the original purpose of {{mono|/dev/zero}}.<ref>{{cite web |title=MAP_ANON description in mmap(2) |url=https://netbsd.gw.com/cgi-bin/man-cgi?mmap |website=NetBSD |access-date=2019-09-09 |archive-date=2019-11-25 |archive-url=https://web.archive.org/web/20191125152633/https://netbsd.gw.com/cgi-bin/man-cgi?mmap |url-status=dead }}</ref>
{{unreferenced-section|date=December 2014}}
/dev/zero was introduced in 1988 by SunOS-4.0 in order to allow a mappable BSS segment for shared libraries using anonymous memory.{{cn|date=December 2014}} In the mid 1990s, HP-UX introduced the mmap() flag MAP_ANONYMOUS that maps anonymous memory directly without a need to open /dev/zero.{{cn|date=December 2014}} Since the late 1990s, MAP_ANONYMOUS is supported by most UNIX versions, removing the original purpose of /dev/zero.{{cn|date=December 2014}}
and then penisàààààʷʷʷʷʷʷʷʷʷʎ̩ʎ̩ʎ̩


==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.
Destroying existing data on a file system partition ([[low-level format]]ting):
dd if=/dev/zero of=/dev/''<destination partition>''


[[Data erasure|Erasing]] a file system partition or drive:
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=http://books.google.com/books?id=AEV1x84DVkUC&lpg=PA27&dq=%2Fdev%2Fzero&pg=PA27#v=onepage&q=/dev/zero&f=false|accessdate=16 July 2014}}</ref>
<source lang="bash">
dd if=/dev/zero of=foobar count=1024 bs=1024
</source>


cp /dev/zero /dev/<destination drive or partition>
Note: The block size value can be given in SI (decimal) values, e.g. in GB, MB, etc. To create a 1 GB file one would simply type:
<source lang="bash">
dd if=/dev/zero of=foobar count=1 bs=1GB
</source>


(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 (computing)|TRIM]].)
Note: Instead of creating a real file with only zero bytes, many file systems also support the creation of [[sparse file]]s which returns zeros upon reading but use less actual space.<ref>[[Sparse file]]</ref>

Creating a 1 [[mebibyte|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 file]]s, 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 [[gibibyte|GiB]] file:

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==
*[[Unix philosophy]]
* [[Unix philosophy]]
*[[Standard streams]]
* [[Standard streams]]
*{{mono|[[:/dev]]}}
* {{mono|[[:/dev]]}}
* {{mono|[[:/dev/null]]}}
* {{mono|[[:/dev/full]]}}


==References==
==References==

Latest revision as of 17:40, 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

[edit]

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

[edit]

/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

[edit]

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.

See also

[edit]

References

[edit]
  1. ^ Mitchell, Mark; Oldham, Jeffrey; Samuel, Alex (2001), "6.5.2 /dev/zero", Advanced Linux Programming, Sams Publishing, p. 136, ISBN 9780735710436
  2. ^ 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
  3. ^ ""C" run-time program bootstrap from SunOS, contributed to CSRG for inclusion in 4.4BSD". TUHS.
  4. ^ "HP-UX 8.0.7 install media". 1992-07-22.
  5. ^ Beal, Chris. "So what the heck is anonymous memory". Oracle Blog. Archived from the original on 2021-04-15. Retrieved 2019-09-09.
  6. ^ "MAP_ANON description in mmap(2)". NetBSD. Archived from the original on 2019-11-25. Retrieved 2019-09-09.