Dd

From LQWiki
Jump to: navigation, search

dd is a utility to create a disk dump by reading every single block on a disk, e.g. your hard drive. However, its architecture is laid out so it can do much more than creating a dump. See the table of contents:

Disk Backup

Create a backup

Say we have a harddisk /dev/sda that we want to backup entirely (sector-by-sector) to a USB volume /dev/sdb1, mounted on /mnt/sdb1. We call this a dump or an image of /dev/sda. The dump shall be named backup.img. Here is the dd command:

dd if=/dev/sda of=/mnt/sdb1/backup.img

In this command, if stands for input file and of for output file.

Restore a backup

To restore this backup, we boot from a live CD and do the command vice versa. This can overwrite all content on your harddisk, this is the intention.

dd if=/mnt/sdb1/backup.img of=/dev/sda

Clone a harddisk

To clone a disk A to B, both disks need to have the same capacity. It is very convenient for USB disks. Say our USB disk source is called /dev/sdb and the target is called /dev/sdc. Do it like this:

dd if=/dev/sdb of=/dev/sdc

Now if sdc has a bigger capacity, this capacity will be lost because the file system is not aware of it.

Transfer a disk image

To transfer a disk image over the network to a computer named target, use

dd if=/dev/sdb | ssh root@target "(cat >backup.img)"

create an iso image of a CD

To create an iso image of a CD, read it block-by-block and save the blocks to a file:

dd if=/dev/cdrom of=cdimage.iso

rescue a file that contains bad blocks

If your favorite movie or song cannot be played any longer because the file is corrupt, you can use dd to ignore the corrupt part:

dd if=movie.avi of=rescued_movie.avi conv=noerror

analyze your disk

DD is great to learn about your system. To analyze your disk by displaying selected blocks, in this case block 1001 of /dev/sdc1 use:

dd if=/dev/sdc1 count=1 skip=1000

To see the first 40 bytes of your first harddisk as a hexdump use

dd if=/dev/sda bs=1 count=40 | hexdump -C

Here, bs stands for blocksize.

Create your own bootloader

To create your own operating system by dumping your bootloader to the boot sector of a bootable disk image use

dd conv=notrunc if=bootloader of=qemu.img

benchmark the throughput of your disks

To benchmark the throughput of your disk /dev/sda1, e.g. for different block sizes, proceed like this:

# dd if=/dev/sdg1 of=/dev/null bs=512 count=1000000
1000000+0 records in
1000000+0 records out
512000000 bytes (512 MB) copied, 4.25186 s, 120 MB/s
# dd if=/dev/sdg1 of=/dev/null bs=4096 count=1000000
1000000+0 records in
1000000+0 records out
4096000000 bytes (4.1 GB) copied, 29.8747 s, 137 MB/s

However, make sure you have read Background:How caching works first otherwise you will be surprised by a mysterious accelleration like this:

# dd if=/dev/sdg1 of=/dev/null bs=512 count=1000000
1000000+0 records in
1000000+0 records out
512000000 bytes (512 MB) copied, 4.25186 s, 120 MB/s
# dd if=/dev/sdg1 of=/dev/null bs=512 count=1000000
1000000+0 records in
1000000+0 records out
512000000 bytes (512 MB) copied, 0.417317 s, 1.2 GB/s

It is best to circumvent the file system cache completely using direct I/O:

# dd iflag=direct if=/dev/sdg1 of=/dev/null bs=512 count=100000
100000+0 records in
100000+0 records out
51200000 bytes (51 MB) copied, 5.01053 s, 10.2 MB/s

Windows pendant

The WinDos pendant of dd is rawrite.

Provided by

Most (all?) Linux distributions incorporate this from the GNU Coreutils: man page

See also

External Links

Related Commands

  • cp - copies files
  • dd_rescue - recover media with errors on it.
  • mv - Moves or renames files
  • rm - Removes files
  • mkdir - Creates a directory
  • install - Copy and set permissions
  • shred - Remove files securely