Dd (Unix) Explained

dd
Author:Ken Thompson
(AT&T Bell Laboratories)
Developer:Various open-source and commercial developers
Programming Language:Plan 9: C
Operating System:Unix, Unix-like, Plan 9, Inferno, Windows
Platform:Cross-platform
Genre:Command
License:coreutils

GPLv3+
Plan 9: MIT License

Repo:coreutils:

dd is a command-line utility for Unix, Plan 9, Inferno, and Unix-like operating systems and beyond, the primary purpose of which is to convert and copy files.[1] On Unix, device drivers for hardware (such as hard disk drives) and special device files (such as /dev/zero and /dev/random) appear in the file system just like normal files; can also read and/or write from/to these files, provided that function is implemented in their respective driver. As a result, can be used for tasks such as backing up the boot sector of a hard drive, and obtaining a fixed amount of random data. The program can also perform conversions on the data as it is copied, including byte order swapping and conversion to and from the ASCII and EBCDIC text encodings.[2]

History

In 1974, the 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),[3] [4] in which it is an abbreviation for "Data Definition".[5] [6] According to Douglas McIlroy, 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.[7] 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.[4]

In 1987, the command is specified in the X/Open Portability Guide issue 2 of 1987. This is inherited by IEEE Std 1003.1-2008 (POSIX), which is part of the Single UNIX Specification.

In 1990, David MacKenzie announced GNU fileutils (now part of coreutils) which includes the dd command;[8] it was written by Paul Rubin, David MacKenzie, and Stuart Kemp. Since 1991, Jim Meyering is its maintainer.[9]

In 1995, Plan 9 2nd edition was released; its command interface was redesigned to use a traditional command-line option style instead of a JCL statement style.

Since at least 1999,[10], a native Win32 port for Microsoft Windows under UnxUtils.[11]

dd is sometimes humorously called "Disk Destroyer", due to its drive-erasing capabilities involving typos.[12]

Usage

The command line syntax of differs from many other Unix programs. It uses the syntax ''option''{{=}}''value'' for its command-line options rather than the more standard -''option value'' or --''option''{{=}}''value'' formats. By default, reads from stdin and writes to stdout, but these can be changed by using the (input file) and (output file) options.

Certain features of will depend on the computer system capabilities, such as 's ability to implement an option for direct memory access. Sending a SIGINFO signal (or a USR1 signal on Linux) to a running process makes it print I/O statistics to standard error once and then continue copying. can read standard input from the keyboard. When end-of-file (EOF) is reached, will exit. Signals and EOF are determined by the software. For example, Unix tools ported to Windows vary as to the EOF: Cygwin uses (the usual Unix EOF) and MKS Toolkit uses (the usual Windows EOF).

The non-standardized parts of dd invocation vary among implementations.

Output messages

On completion, prints to the stderr stream about statistics of the data transfer. The format is standardized in POSIX. 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.

Block size

A block is a unit measuring the number of bytes that are read, written, or converted at one time. Command-line options can specify a different block size for input/reading compared to output/writing, though the block size option will override both and . The default value for both input and output block sizes is 512 bytes (the traditional block size of disks, and POSIX-mandated size of "a block"). The option for copying is measured in blocks, as are both the count for reading and count for writing. Conversion operations are also affected by the "conversion block size" .

The value provided for block size options is interpreted as a decimal (base 10) integer number of bytes. It can also contain suffixes to indicate that the block size is an integer number of larger units than bytes. POSIX only specifies the suffixes (blocks) for 512 and (kibibytes) for 1024. Implementation differ on the additional suffixes they support: (Free) BSD uses lowercase (mebibytes), (gibibytes), and so on for tebibytes, exbibytes, pebibytes, zebibytes, and yobibytes, while GNU uses and for the same units, with,, and used for their SI unit counterparts (kilobytes). For example, for GNU, indicates a blocksize of 16 mebibytes (16777216 bytes) and specifies 3000 bytes.

Additionally, some implementations understand the character as a multiplication operator for both block size and count parameters. For example, is interpreted as 2 × 80 × 18 × 512 =, the exact size of a 1440 KiB floppy disk. This is required in POSIX. For implementations that do not support this feature, the POSIX shell arithmetic syntax of bs=$((2*80*18))b may be used.

Block size has an effect on the performance of copying commands. Doing many small reads or writes is often slower than doing fewer large ones. Using large blocks requires more RAM and can complicate error recovery. When is used with variable-block-size devices such as tape drives or networks, the block size may determine the tape record size or packet size, depending on the network protocol used.

Uses

The command can be used for a variety of purposes. For plain-copying commands it tends to be slower than the domain-specific alternatives, but it excels at its unique ability to "overwrite or truncate a file at any point or seek in a file", a fairly low-level interface to the Unix file API.[13]

The examples below assume the use of GNU dd, mainly in the block size argument. To make them portable, replace e.g. with the shell arithmetic expression or (written equivalently with a bit shift).

Data transfer

can duplicate data across files, devices, partitions and volumes. The data may be input or output to and from any of these; but there are important differences concerning the output when going to a partition. Also, during the transfer, the data can be modified using the options to suit the medium. (For this purpose, however, is slower than .)

Data transfer forms of
Creates an ISO disk image from a CD-ROM, DVD or Blu-ray disc.[14]
Restores a hard disk drive (or an SD card, for example) from a previously created image.
Create an image of the partition sdb2, using a 64 MiB block size.
Clones one partition to another.
Clones a hard disk drive "ad0" to "ad1".

The option means to keep going if there is an error, while the option causes output blocks to be padded.

In-place modification

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

The conversion option means do not truncate the output file — that is, if the output file already exists, just replace the specified bytes and leave the rest of the output file alone. Without this option, would create an output file 512 bytes long.

Master boot record backup and restore

The example above can also be used to back up and restore any region of a device to a file, such as a master boot record.

To duplicate the first two sectors of a floppy disk:

Disk wipe

See main article: Data erasure.

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, conversion option is not required as it has no effect when the 's output file is a block device.[15]

The option 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.[16] 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. When available, this is both faster than using dd, and more secure. On Linux machines it is accessible via the hdparm command's option.

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

Data recovery

Data recovery involves reading from a drive with some parts potentially inaccessible. is a good fit with this job with its flexible skipping and other low-level settings. The vanilla, 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 [17] was written in October 1999. It did away with the conversion functionality of, 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 script was written to automate the process of using, keeping track of what areas have been read on its own.[18]

In 2004, GNU wrote a separate utility, unrelated to, called . It has a more sophisticated dynamic block-size algorithm and keeps track of what has been read internally. The authors of both and consider it superior to their implementation.[19] To help distinguish the newer GNU program from the older script, alternate names are sometimes used for GNU's, including (the name on freecode.com and freshmeat.net), (Debian package name), and (openSUSE package name).

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

Benchmarking drive performance

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

Generating a file with random data

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

Converting a file to upper case

To convert a file to uppercase:

Progress indicator

Being a program mainly designed as a filter, normally does not provide any progress indication. This can be overcome by sending an signal to the running GNU process (on BSD systems), resulting in printing the current number of transferred blocks.

The following one-liner results in continuous output of progress every 10 seconds until the transfer is finished, when is replaced by the process-id of :

Newer versions of GNU support the option, which enables periodic printing of transfer statistics to stderr.[20]

Forks

dcfldd

is a fork of GNU 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.[21] [22] [23] Compared to, 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.[24]

dc3dd

is another fork of GNU 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., the last release was 7.3.1 from April 2023.[25]

See also

External links

Notes and References

  1. Web site: POSIX standard: dd invocation . Austin Group . 2016-09-29 . 2010-03-10 . https://web.archive.org/web/20100310213820/http://www.opengroup.org/onlinepubs/9699919799/utilities/dd.html . live .
  2. Web site: Chessman . Sam . How and when to use the dd command? . https://web.archive.org/web/20080214125249/http://www.codecoffee.com/tipsforlinux/articles/036.html . 14 Feb 2008 . 2008-02-19 . CodeCoffee . dead.
  3. Re: origin of the UNIX dd command . Dennis . Ritchie . Feb 17, 2004 . alt.folklore.computers . c0s1he$1atuh9$1@ID-156882.news.uni-berlin.de . dd was always named after JCL dd cards. . January 10, 2016 . January 22, 2011 . http://arquivo.pt/wayback/20110122130054/https://groups.google.com/forum/#!msg/alt.folklore.computers/HAWoZ8g-xYk/HDUVxwTVLKAJ . live .
  4. Web site: dd . Eric S. . Raymond . 2008-02-19 . 2018-12-13 . https://web.archive.org/web/20181213051044/https://www.catb.org/jargon/html/D/dd.html . live .
  5. Book: Struble . George . Assembler language programming: the IBM System/360 . 1969 . Reading, Mass., Addison-Wesley Pub. Co . 123 .
  6. Re: etymology of the Unix "dd" command . Barry . Shein . Apr 22, 1990 . alt.folklore.computers . 1990Apr22.191928.11180@world.std.com . 2016-07-14 . 2023-10-24 . https://web.archive.org/web/20231024083144/https://groups.google.com/g/alt.folklore.computers/c/K4uzWBugSPE/m/GwSgDUEU_3wJ . live .
  7. M. D. . McIlroy . Doug McIlroy . 1987 . A Research Unix reader: annotated excerpts from the Programmer's Manual, 1971–1986 . CSTR . 139 . Bell Labs.
  8. Web site: GNU file utilities release 1.0 . groups.google.com . 2023-04-28 . 2023-04-28 . https://web.archive.org/web/20230428222649/https://groups.google.com/g/gnu.utils.bug/c/CviP42X_hCY/m/YssXFn-JrX4J . live .
  9. Web site: GNU's Who . 2023-04-28 . 2023-04-28 . https://web.archive.org/web/20230428041041/http://www.gnu.org/people/people.html#j . live .
  10. Web site: Native Win32 ports of some GNU utilities . 15 August 2000. https://web.archive.org/web/20000815200240/http://www.weihenstephan.de:80/~syring/win32/UnxUtils.html . 2000-08-15 .
  11. Web site: Native Win32 ports of some GNU utilities. unxutils.sourceforge.net. 2022-02-23. 2006-02-09. https://web.archive.org/web/20060209022842/http://unxutils.sourceforge.net/. live.
  12. Web site: 2018-07-05. How to use dd in Linux without destroying your disk. 2020-10-11. Opensource.com. en. 2020-10-11. https://web.archive.org/web/20201011054921/https://opensource.com/article/18/7/how-use-dd-linux. live.
  13. Web site: Gilles . cloning - dd vs cat – is dd still relevant these days? . Unix & Linux Stack Exchange . 2011 . 2020-04-24 . 2023-10-24 . https://web.archive.org/web/20231024083153/https://unix.stackexchange.com/questions/12532/dd-vs-cat-is-dd-still-relevant-these-days/12538#12538 . live .
  14. Web site: Creating an ISO image from a CD, DVD, or BD . ArchWiki . April 18, 2022 . April 18, 2022 . https://web.archive.org/web/20220418171303/https://wiki.archlinux.org/title/Optical_disc_drive#Creating_an_ISO_image_from_a_CD,_DVD,_or_BD . live .
  15. Web site: linux - Why using conv=notrunc when cloning a disk with dd? . Stack Overflow . 2013-12-11 . 2014-03-24 . 2014-03-24 . https://web.archive.org/web/20140324201126/http://stackoverflow.com/questions/20526198/why-using-conv-notrunc-when-cloning-a-disk-with-dd . live .
  16. Wright . Craig S. . Kleiman . Dave . S. . Shyaam Sundhar R. . Sekar . R. . Pujari . Arun K. . Overwriting Hard Drive Data: The Great Wiping Controversy . 10.1007/978-3-540-89862-7_21 . 243–257 . Springer . Lecture Notes in Computer Science . Information Systems Security, 4th International Conference, ICISS 2008, Hyderabad, India, December 16-20, 2008. Proceedings . 5352 . 2008.
  17. Web site: dd_rescue. garloff.de. 2006-11-10. 2001-05-16. https://web.archive.org/web/20010516091650/http://www.garloff.de/kurt/linux/ddrescue/. live.
  18. Web site: dd_rhelp author's repository. LAB Valentin. 19 September 2011. Important note : For some times, dd_rhelp was the only tool (AFAIK) that did this type of job, but since a few years, it is not true anymore: Antonio Diaz did write a ideal replacement for my tool: GNU 'ddrescue'.. 13 May 2008. 16 May 2008. https://web.archive.org/web/20080516231947/http://www.kalysto.org/utilities/dd_rhelp/index.en.html. live.
  19. Web site: Ddrescue - GNU Project - Free Software Foundation (FSF). gnu.org. 2016-07-22. 2021-07-02. https://web.archive.org/web/20210702144117/https://www.gnu.org/software/ddrescue/ddrescue.html. live.
  20. Web site: GNU Coreutils: dd invocation . The GNU Operating System and the Free Software Movement . 2019-08-26 . 2019-08-22 . https://web.archive.org/web/20190822014954/http://www.gnu.org/software/coreutils/manual/html_node/dd-invocation.html . live .
  21. Web site: DCFLDD at Source Forge . Source Forge . 2013-08-17 . 2013-08-02 . https://web.archive.org/web/20130802114625/http://dcfldd.sourceforge.net/ . live .
  22. Book: Jeremy Faircloth, Chris Hurley . Penetration Tester's Open Source Toolkit . 2007 . Syngress . 9780080556079 . 470–472.
  23. Book: Jack Wiles, Anthony Reyes . The Best Damn Cybercrime and Digital Forensics Book Period . 2011 . Syngress . 9780080556086 . 408–411.
  24. Web site: dcfldd: Enhanced version of dd for forensics and security . GitHub . 2020-11-19 . 2020-10-31 . https://web.archive.org/web/20201031000409/https://github.com/resurrecting-open-source-projects/dcfldd . live .
  25. Web site: dc3dd . SourceForge . 25 April 2023 . en . 24 April 2020 . 25 February 2020 . https://web.archive.org/web/20200225233213/https://sourceforge.net/projects/dc3dd/ . live .