Dd (command)

From Christoph's Personal Wiki
Revision as of 01:46, 7 May 2018 by Christoph (Talk | contribs) (Examples)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
The correct title of this article is dd. The initial letter is capitalized due to technical restrictions.

dd is a command-line utility whose primary purpose is the low-level copying and conversion of files.

see: wikipedia:dd (Unix) for more details.

Usage

dd [options]

operands

if=file 
Input File: Read from file instead from standard input.
of=file 
Output File: Write to file instead to standard output. See also the keyword notrunc.
ibs=bytes 
Input Block Size: Read bytes bytes at once.
obs=bytes 
Output Block Size: Write bytes bytes at once.
bs=bytes 
Block Size: A shortcut for ibs=bytes obs=bytes. If the user does not provide a block size, 512 bytes is used.[1] One can determine the block size of a storage medium by assuming a block size, writing one block and checking for an error message. For most modern drives, the optimum value for this operand for maximum read/write performance is 4k (i.e. bs=4k)[2]
count=blocks 
Count: copy only this many blocks from the input to the output, then stop.
skip=blocks 
When starting to read from input, skip blocks number of blocks of size ibs.
seek=blocks 
When starting to write to output, skip blocks number of blocks of size obs.
conv=keywords 
Convert the file according to a comma-separated list of keywords.
cbs=bytes 
Convert Block Size: Convert bytes bytes at once.

conv

When specifying conv as parameter the following keywords may be used:

ascii 
Convert from EBCDIC to ASCII.
ebcdic 
Convert from ASCII to EBCDIC.
ibm 
Convert from ASCII to an alternative EBCDIC.
block 
Fill datasets which are terminated by a newline-character with space-characters to fit size of cbs.
unblock 
Replace trailing space-characters in datasets of size cbs with newline-characters.
lcase 
Change uppercase characters to lowercase.
ucase 
Change lowercase characters to uppercase.
notrunc 
Do not truncate output file to zero bytes before writing to it. If the existing output file is shorter than the amount of data to be written to it, this will cause the written data to overwrite the initial portion, leaving the remainder intact.
swab 
Swap every pair of input bytes.
noerror 
Ignore reading errors and continue.
sync 
Pad every input block with null bytes if it is shorter than the size specified. If used with block or unblock, pad with space characters instead.

Notes and units

On various systems the option --version is supported. dd will then output its version number and quit.

file may be any real file or any block-device file.

On certain systems bytes may be specified with multiplicative units. This units may then be:

c 
Character: 1
w 
Word: 2
b 
Block: 512
kB 
Decimal kilobytes: 1,000 bytes
k 
Binary kilobytes (kibibytes): 1,024 bytes
MB 
Decimal megabytes: 1,000,000 bytes
M 
Binary megabytes (mebibytes): 1,048,576 bytes (1,024×1,024)

This may be carried on similarly with G (gigabyte), T (terabyte), P (petabyte), E (exabyte), Z (zettabyte), Y (yottabyte). The standard "IEEE Std 1003.1" only requires that the 'b' and 'k' multipliers be supported, and does not specify the meaning of any other multipliers. Also, multiple numbers may be provided separated by 'x'. These numbers are multiplied together.

Examples

To create an ISO image file from a CD. Insert the source cd and unmount it first if auto CD mount is enabled

dd if=/dev/cdrom of=/tmp/image.iso

Note: The device might also be called /dev/dvd or /dev/scd0 (for a SCSI-drive).

To create an image file named floppy.img of a floppy disk in the drive whose block-device name is /dev/fd0 (as the first floppy device is on Linux), one may invoke dd in the following way:

dd if=/dev/fd0 of=floppy.img

Or to copy the img file back to a floppy:

dd if=floppy.img of=/dev/fd0 bs=16k

To create a file with name reallylargefile with the size of 1 GB, filled with random data, do this (1G = 1073741824, 1073741824 / 512 = 2097152 (the default block size is 512)):

dd if=/dev/random of=reallylargefile count=2097152

To fill the file with NULL characters, use /dev/zero instead of /dev/random. This can be used to mount a filesystem if you have a FAT32 device, but need the flexibility of EXT2 - as follows to create a 10GB image:

dd if=/dev/zero of=my.new.virtual.harddisk bs=1k count=10MB
mke2fs my.new.virtual.harddisk  # (reply yes when it says it's not a block device)
mkdir /mnt/virtual
mount -o loop my.new.virtual.harddisk /mnt/virtual/

To create a 10GB sparse file which doesn't allocate any actual space (if the filesystem supports this feature):

dd if=/dev/zero of=sparsefile.img bs=1 seek=10G count=0

To increase writing performance obs can be increased:

dd if=/dev/random of=reallylargefile obs=4096

This will write the first partition of the first harddisk to the file mywindowspartition.image.

dd if=/dev/hda1 of=mywindowspartition.image

These examples are Linux-centric: on other platforms the device names may be different.

View this article for many more examples of the Linux dd command.

Master boot record backup and restore

It is possible to repair a master boot record. It can be transferred to and from a repair file.

  • To duplicate the first two sectors of a floppy drive:
$ dd if=/dev/fd0 of=MBRboot.img bs=512 count=2
  • To create an image of the entire x86 master boot record (including a MS-DOS partition table and MBR magic bytes):
$ dd if=/dev/sda of=MBR.img bs=512 count=1
  • To create an image of only the boot code of the master boot record (without the partition table and without the magic bytes required for booting):
$ dd if=/dev/sda of=MBR_boot.img bs=446 count=1
Data modification

dd can modify data in place.

  • For example, this overwrites the first 512 bytes of a file with null bytes:
$ dd if=/dev/zero of=path/to/file bs=512 count=1 conv=notrunc

The notrunc 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, dd would create an output file 512 bytes long.

  • To duplicate a disk partition as a disk image file on a different partition:
$ dd if=/dev/sdb2 of=partition.image bs=4096 conv=noerror
Disk wipe

Main article: Data erasure For security reasons, it is sometimes necessary to have a disk wipe of a discarded device.

  • To wipe a disk by writing zeros to it, dd can be used this way:
$ dd if=/dev/zero of=/dev/sda bs=16M
  • Another approach could be to wipe a disk by writing random data to it:
$ dd if=/dev/urandom of=/dev/sda bs=16M
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:
$ dd if=/dev/zero bs=1024 count=1000000 of=file_1GB
$ dd if=file_1GB of=/dev/null bs=1024
Generating a file with random data
  • To make a file of 100 random bytes using the kernel random driver:
$ dd if=/dev/urandom of=myrandom bs=100 count=1
Converting a file to upper case
  • To convert a file to uppercase:
$ dd if=filename of=filename1 conv=ucase,notrunc

Anti-examples

WARNING: This article or section describes techniques which can be dangerous for your computer's hardware or the data on it. I have tested everything I describe herein on my personal computers. However, absolutely no guarantee can be made that it will work for you. Proceed with caution!

WARNING: Do NOT try these examples; they will destroy data!

The following examples are provided to warn about the dangers of dd, if used incorrectly. Trying any of these commands with the proper privileges will almost certainly result in major data loss, and may make the system unusable.

This overwrites the complete first hard disk with null bytes, erasing it (do this if you want to sell a hard drive that contains company secrets):

dd if=/dev/zero of=/dev/hda
# ~OR ~
dd if=/dev/zero of=/dev/hda bs=10MB conv=noerror,sync

This overwrites the first few blocks of the first hard disk with the file, resulting in a loss of the partition table:

dd if=funnysong.mp3 of=/dev/hda

This will completely corrupt an entire hard disk (/dev/dsp is the sound player/recorder):

dd if=/dev/dsp of=/dev/hda

This will overwrite an entire disk with pseudorandom data, making its initial contents relatively irrecoverable

dd if=/dev/urandom of=/dev/hda

These examples are Linux-centric. On other platforms the device names may be different.

References

  1. The Open Group Base Specifications Issue 6. IEEE Std 1003.1, 2004 Edition (accessed: 2007-02-12).
  2. Learn The DD Command Revised. LinuxQuestions.org. 2005-01 (accessed: 2007-02-12).

See also

External links