Dd (Unix)

Revision as of 07:29, 23 April 2025 by imported>Stesmo (→‎External links: Pruned EL links per WP:EL)
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

Template:Lowercase title Template:Short description {{#invoke:Infobox|infobox}}Template:Template other{{#invoke:Check for unknown parameters | check | showblankpositional=1 | unknown = Template:Main other | preview = Page using Template:Infobox software with unknown parameter "_VALUE_"|ignoreblank=y | AsOf | author | background | bodystyle | caption | collapsetext | collapsible | developer | discontinued | engine | engines | genre | included with | language | language count | language footnote | latest preview date | latest preview version | latest release date | latest release version | latest_preview_date | latest_preview_version | latest_release_date | latest_release_version | licence | license | logo | logo alt | logo caption | logo upright | logo size | logo title | logo_alt | logo_caption | logo_upright | logo_size | logo_title | middleware | module | name | operating system | operating_system | other_names | platform | programming language | programming_language | released | replaced_by | replaces | repo | screenshot | screenshot alt | screenshot upright | screenshot size | screenshot title | screenshot_alt | screenshot_upright | screenshot_size | screenshot_title | service_name | size | standard | title | ver layout | website | qid }}Template:Main other dd is shell command for reading, writing and converting file data. Originally developed for Unix, it has been implemented on many other environments including Unix-like operating systems, Windows, Plan 9 and Inferno.<ref name="OpenGroup-dd-man">{{#invoke:citation/CS1|citation |CitationClass=web }}</ref>

The command can be used for many purposes. For relatively simple copying operations, it tends to be slower than domain-specific alternatives, but it excels at overwriting or truncating a file at any point or seeking in a file.<ref name=relevant>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref>

The command supports reading and writing files, and if a driver is available to support file-like access, the command can access devices too. Such access is typically supported on Unix-based systems that provide file-like access to devices (such as storage) and special device files (such as /dev/zero and /dev/random). Therefore, the command can be used for tasks such as backing up the boot sector of a drive, and obtaining random data.

The command can also support converting data while copying; including byte order swapping and converting between ASCII and EBCDIC text encodings.<ref name="tfl-dd">{{#invoke:citation/CS1|citation |CitationClass=web }}</ref>

dd is sometimes humorously called "Disk Destroyer", due to its drive-erasing capabilities involving typos.<ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref>

HistoryEdit

In 1974, the <syntaxhighlight lang="text" class="" style="" inline="1">dd</syntaxhighlight> command appeared as part of Version 5 Unix. According to Dennis Ritchie, the name is an allusion to the DD statement found in IBM's Job Control Language (JCL),<ref>Template:Cite newsgroup</ref><ref name="jargon-dd">{{#invoke:citation/CS1|citation |CitationClass=web }}</ref> where DD is short for data definition.<ref>Template:Cite book</ref><ref>Template:Cite newsgroup</ref> According to Douglas McIlroy, <syntaxhighlight lang="text" class="" style="" inline="1">dd</syntaxhighlight> was "originally intended for converting files between the ASCII, little-endian, byte-stream world of DEC computers and the EBCDIC, big-endian, blocked world of IBM"; thus, explaining the cultural context of its syntax.<ref>Template:Cite tech report</ref> Eric S. Raymond believes "the interface design was clearly a prank", due to the command's syntax resembling a JCL statement more than other Unix commands do.<ref name="jargon-dd"/>

In 1987, the <syntaxhighlight lang="text" class="" style="" inline="1">dd</syntaxhighlight> command is specified in the X/Open Portability Guide issue 2 of 1987Template:Citation needed. This is inherited by IEEE Std 1003.1-2008 (POSIX)Template:Citation needed, which is part of the Single UNIX Specification.<ref name=SUS>Template:Man</ref>

In 1990, David MacKenzie announced GNU fileutils (now part of coreutils) which includes the dd command;<ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref> it was written by Paul Rubin, David MacKenzie, and Stuart Kemp.<ref name=lin>Template:Man</ref> Since 1991, Jim Meyering is its maintainer.<ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref>

In 1995, Plan 9 2nd edition was released with a <syntaxhighlight lang="text" class="" style="" inline="1">dd</syntaxhighlight> command with a more traditional command-line option style than the JCL statement style.<ref>Template:Man</ref>

Since at least 1999, UnxUtils has provided a native implementation for the Windows platform.<ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref><ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref>

UseEdit

The command line interface significantly differs from most modern shell commands in that an option is formatted as option=value instead of the more typical syntax that denotes an option with a dash prefix such as: -x, -y value, --abc, --def value.

By default, <syntaxhighlight lang="text" class="" style="" inline="1">dd</syntaxhighlight> reads from standard input and writes to standard output, but input and output can be overridden. Option <syntaxhighlight lang="text" class="" style="" inline="1">if</syntaxhighlight> specifies an input file and option <syntaxhighlight lang="text" class="" style="" inline="1">of</syntaxhighlight> specifies an output file.

Non-standardized aspects of <syntaxhighlight lang="text" class="" style="" inline="1">dd</syntaxhighlight> depend on the underlying system or implementation, including:

Output messagesEdit

On completion, <syntaxhighlight lang="text" class="" style="" inline="1">dd</syntaxhighlight> writes statistics to standard error. The format is standardized in POSIX.<ref name=SUS/>Template:Rp The manual page for GNU dd does not describe this format, but the BSD manuals do. Each of the "Records in" and "Records out" lines shows the number of complete blocks transferred + the number of partial blocks, e.g. because the physical medium ended before a complete block was read, or a physical error prevented reading the complete block.

If <syntaxhighlight lang="text" class="" style="" inline="1">dd</syntaxhighlight> receives a SIGINFO signal while it's running Template:Endash typically triggered by the user pressing Template:Key Template:Endash it writes intermediate statistics to standard error and continues processing.

Block sizeEdit

The command processes data in blocks. The default size is 512 (the POSIX-mandated size and a common legacy size for disk hardware) but can be specified via command-line options. Option <syntaxhighlight lang="text" class="" style="" inline="1">bs</syntaxhighlight> specifies the size for both input (read) and output (write) operations. Alternatively, option <syntaxhighlight lang="text" class="" style="" inline="1">ibs</syntaxhighlight> specifies the size for input operations and <syntaxhighlight lang="text" class="" style="" inline="1">obs</syntaxhighlight> for output operations. Option <syntaxhighlight lang="text" class="" style="" inline="1">cbs</syntaxhighlight> affects conversion operations.

Options <syntaxhighlight lang="text" class="" style="" inline="1">count</syntaxhighlight>, <syntaxhighlight lang="text" class="" style="" inline="1">skip</syntaxhighlight> and <syntaxhighlight lang="text" class="" style="" inline="1">seek</syntaxhighlight> specify a number of blocks: maximum to read, to start reading at offset from the start of the input, and to start writing at offset from the start of the output, respectively.<ref name=SUS/>Template:Rp

A block size option value is specified as a whole decimal number of bytes with an optional suffix to indicate a multiplier. POSIX requires suffixes <syntaxhighlight lang="text" class="" style="" inline="1">b</syntaxhighlight> (blocks) for 512 and <syntaxhighlight lang="text" class="" style="" inline="1">k</syntaxhighlight> (kibibytes) for 1024,<ref name=SUS/>Template:Rp but implementations differ on other suffixes. (Free) BSD uses <syntaxhighlight lang="text" class="" style="" inline="1">m</syntaxhighlight> for mebibytes, <syntaxhighlight lang="text" class="" style="" inline="1">g</syntaxhighlight> for gibibytes and so on for larger power of two units.<ref name=bsd>Template:Man</ref> GNU uses <syntaxhighlight lang="text" class="" style="" inline="1">M</syntaxhighlight> and <syntaxhighlight lang="text" class="" style="" inline="1">G</syntaxhighlight> and so on for these units and uses <syntaxhighlight lang="text" class="" style="" inline="1">kB</syntaxhighlight>, <syntaxhighlight lang="text" class="" style="" inline="1">MB</syntaxhighlight>, and <syntaxhighlight lang="text" class="" style="" inline="1">GB</syntaxhighlight> for SI units.<ref name=lin/> For example, for GNU dd, <syntaxhighlight lang="text" class="" style="" inline="1">bs=16M</syntaxhighlight> indicates a size of 16 mebibytes (16777216 bytes) and <syntaxhighlight lang="text" class="" style="" inline="1">bs=3kB</syntaxhighlight> specifies 3000 bytes.

For POSIX compliance, some implementations interpret the <syntaxhighlight lang="text" class="" style="" inline="1">x</syntaxhighlight> character as a multiplication operator for both block size and count option values. For example, <syntaxhighlight lang="text" class="" style="" inline="1">bs=2x80x18b</syntaxhighlight> is interpreted as 2 × 80 × 18 × 512 = Template:Val, the size of a 1440 KiB floppy disk.<ref name=SUS/>Template:Rp For implementations that do not support this feature, the POSIX shell arithmetic syntax of <syntaxhighlight lang=bash inline>bs=$((2*80*18))b</syntaxhighlight> may be used.

Block size affects performance. Many small reads and writes is often slower than fewer, larger ones. On the downside, larger blocks require more RAM and can complicate error recovery.

When used with a variable block size device such as a tape drive or a network, the block size may determine the tape record size or network packet size, depending on the network protocol.

ExamplesEdit

The examples below apply to many implementations, but are specifically written for GNU dd. Generally, the only difference between implementations is block size values and can be portable by using shell arithmetic expression instead of a size multiplier suffix. For example, instead of <syntaxhighlight lang="text" class="" style="" inline="1">bs=64M</syntaxhighlight> use <syntaxhighlight lang="bash" class="" style="" inline="1">bs=$((64*1024*1024))</syntaxhighlight> or <syntaxhighlight lang="bash" class="" style="" inline="1">bs=$((64 << 20))</syntaxhighlight>.

Data transferEdit

The command can duplicate data across files, devices, partitions and volumes, and it can transform data during transfer as specified via option <syntaxhighlight lang="text" class="" style="" inline="1">conv</syntaxhighlight>. In some cases, data transfer is faster with <syntaxhighlight lang="text" class="" style="" inline="1">cat</syntaxhighlight>.<ref name="relevant" />

To create an ISO disk image from a CD-ROM, DVD or Blu-ray disc:<ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref>

<syntaxhighlight lang="bash"> blocks=$(isosize -d 2048 /dev/sr0) dd if=/dev/sr0 of=isoimage.iso bs=2048 count=$blocks status=progress </syntaxhighlight>

To restore a drive from an image file:

<syntaxhighlight lang="bash"> dd if=system.img of=/dev/sdc bs=64M conv=noerror </syntaxhighlight>

To create an image of partition sdb2, using a 64 MiB block size:

<syntaxhighlight lang="bash"> dd if=/dev/sdb2 of=partition.image bs=64M conv=noerror </syntaxhighlight>

To clone one partition to another:

<syntaxhighlight lang="bash"> dd if=/dev/sda2 of=/dev/sdb2 bs=64M conv=noerror </syntaxhighlight>

To clone drive ad0 to ad1; ignoring any errors:

<syntaxhighlight lang="bash"> dd if=/dev/ad0 of=/dev/ad1 bs=64M conv=noerror </syntaxhighlight>

In-place modificationTemplate:AnchorEdit

The command can modify data in place. For example, this overwrites the first 512 bytes of a file with null bytes:

<syntaxhighlight lang="bash"> dd if=/dev/zero of=path/to/file bs=512 count=1 conv=notrunc </syntaxhighlight>

Option <syntaxhighlight lang="text" class="" style="" inline="1">conv=notrunc</syntaxhighlight> requests to not truncate the output file. That is, if the output file already exists, replace the specified bytes and leave the rest of the output file as-is. Without this option, the command would create an output file 512 bytes long.

Master boot record backup and restoreEdit

The example above can also be used to backup and restore any region of a device to a file; including a master boot record. To duplicate the first two sectors of a floppy disk:

<syntaxhighlight lang="bash"> dd if=/dev/fd0 of=MBRboot.img bs=512 count=2 </syntaxhighlight>

Disk wipeEdit

{{#invoke:Labelled list hatnote|labelledList|Main article|Main articles|Main page|Main pages}}

For security reasons, it is sometimes necessary to have a disk wipe of a discarded device. This can be achieved by a "data transfer" from the Unix special files.

When compared to the data modification example above, <syntaxhighlight lang="text" class="" style="" inline="1">notrunc</syntaxhighlight> conversion option is not required as it has no effect when the output file is a block device.<ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref>

Option <syntaxhighlight lang="text" class="" style="" inline="1">bs=16M</syntaxhighlight> makes dd read and write 16 mebibytes at a time. For modern systems, an even greater block size may be faster. Note that filling the drive with random data may take longer than zeroing the drive, because the random data must be created by the CPU, while creating zeroes is very fast. On modern hard-disk drives, zeroing the drive will render most data it contains permanently irrecoverable.<ref>Template:Cite conference</ref> However, with other kinds of drives such as flash memories, much data may still be recoverable by data remanence.

Modern hard disk drives contain a Secure Erase command designed to permanently and securely erase every accessible and inaccessible portion of a drive. It may also work for some solid-state drives (flash drives). As of 2017, it does not work on USB flash drives nor on Secure Digital flash memories.Template:Citation needed When available, this is both faster than using dd, and more secure.Template:Citation needed On Linux machines it is accessible via the hdparm command's <syntaxhighlight lang="text" class="" style="" inline="1">--security-erase-enhanced</syntaxhighlight> option.

The shred program offers multiple overwrites, as well as more secure deletion of individual files.

Data recoveryEdit

Data recovery involves reading from a drive with some parts potentially inaccessible. The command is a good fit with this job with its flexible skipping (<syntaxhighlight lang="text" class="" style="" inline="1">seek</syntaxhighlight>) and other low-level settings. The vanilla <syntaxhighlight lang="text" class="" style="" inline="1">dd</syntaxhighlight>, however, is clumsy to use as the user has to read the error messages and manually calculate the regions that can be read. The single block size also limits the granularity of the recovery, as a trade-off has to be made: either use a small one for more data recovered or use a large one for speed.

A C program called dd_rescue<ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref> was written in October 1999. It did away with the conversion functionality of <syntaxhighlight lang="text" class="" style="" inline="1">dd</syntaxhighlight>, and supports two block sizes to deal with the dilemma. If a read using a large size fails, it falls back to the smaller size to gather as much as data possible. It can also run backwards. In 2003, a dd_rhelp script was written to automate the process of using dd_rescue, keeping track of what areas have been read on its own.<ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref>

In 2004, GNU wrote a separate utility, unrelated to <syntaxhighlight lang="text" class="" style="" inline="1">dd</syntaxhighlight>, called ddrescue. It has a more sophisticated dynamic block-size algorithm and keeps track of what has been read internally. The authors of both dd_rescue and dd_rhelp consider it superior to their implementation.<ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref> To help distinguish the newer GNU program from the older script, alternate names are sometimes used for GNU's ddrescue, including addrescue (the name on freecode.com and freshmeat.net), gddrescue (Debian package name), and gnu_ddrescue (openSUSE package name).

Another open-source program called savehd7 uses a sophisticated algorithm, but it also requires the installation of its own programming-language interpreter.

Benchmark drive performanceEdit

To make drive benchmark test and analyze the sequential (and usually single-threaded) system read and write performance for 1024-byte blocks:

  • Write performance: dd if=/dev/zero bs=1024 count=1000000 of=1GB_file_to_write
  • Read performance: dd if=1GB_file_to_read of=/dev/null bs=1024

Generate a file with random dataEdit

To make a file of 100 random bytes using the random driver:

<syntaxhighlight lang="bash"> dd if=/dev/urandom of=myrandom bs=100 count=1 </syntaxhighlight>

Convert a file to upper caseEdit

To convert a file to uppercase:

<syntaxhighlight lang="bash"> dd if=filename of=filename1 conv=ucase,notrunc </syntaxhighlight>

Progress feedbackEdit

On request, the command reports progress. When it receives signal <syntaxhighlight lang="text" class="" style="" inline="1">USR1</syntaxhighlight> (<syntaxhighlight lang="text" class="" style="" inline="1">INFO</syntaxhighlight> on BSD systems), it writes the number of transferred blocks to standard error.

The following bash script requests progress every 10 seconds until the transfer completes. The text PID stands for the <syntaxhighlight lang="text" class="" style="" inline="1">dd</syntaxhighlight> process identifier.

<syntaxhighlight lang="bash"> while kill -USR1 PID ; do sleep 10 ; done </syntaxhighlight>

Newer versions of GNU dd support the option <syntaxhighlight lang="text" class="" style="" inline="1">status=progress</syntaxhighlight> which enables periodic status feedback.<ref name=gnu_invok>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref>

ForksEdit

dcfldd Template:AnchorEdit

dcfldd is a fork of GNU dd that is an enhanced version developed by Nick Harbour, who at the time was working for the United States' Department of Defense Computer Forensics Lab.<ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref><ref>Template:Cite book</ref><ref>Template:Cite book</ref> Compared to <syntaxhighlight lang="text" class="" style="" inline="1">dd</syntaxhighlight>, dcfldd allows more than one output file, supports simultaneous multiple checksum calculations, provides a verification mode for file matching, and can display the percentage progress of an operation. As of February 2024, the last release was 1.9.1 from April 2023.<ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref>

dc3ddEdit

dc3dd is another fork of GNU dd from the United States Department of Defense Cyber Crime Center (DC3). It can be seen as a continuation of the dcfldd, with a stated aim of updating whenever the GNU upstream is updated. Template:As of, the last release was 7.3.1 from April 2023.<ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref>

See alsoEdit

Template:Portal

ReferencesEdit

Template:ReflistTemplate:Sister projectTemplate:Optical disc image software Template:Unix commands Template:Plan 9 commands Template:Core Utilities commands

Template:Backup software