Jul 072011
 

tux dd seem an unfriendly command, but if you start to use it, you’ll see that it’s a powerful command able to do many different things, backup a partition, CD or USB stick for example or do some simple tests on the speed of your disks or your CPU.

The man page say:

dd is an application that will “convert and copy a file”

But let’s see some trick with it.


The GNU clone of dd is part of fileutils package, and i’d say that any Linux should have it installed.
Unlike most Unix commands, dd uses a keyword=value format for its parameters.

Basic Usage

The dd utility copies the specified input file to the specified output with possible conversions. The standard input and output are used by default. The input and output block sizes may be specified to take advantage of raw physical I/O. Sizes are specified in bytes; a number may end with k, b, or w to specify multiplication by 1024, 512, or 2, respectively. Numbers may also be separated by x to indicate multiplication.

So the basic options are:

if= file Specifies the input path. Standard input is the default.

of= file Specifies the output path. Standard output is the default.
Example 1

# dd if=/dev/sda of=/dev/sdb

This command will do the exact copy of /dev/sda in /dev/sdb.

Example 2

#dd if=/dev/dvd of=dvd.iso

This command will do a copy of the DVD in the file dvd.iso

Example 3

#dd if=/dev/zero of=/dev/sda

Fill partition /dev/sda with zero, useful to wipe out a phisical (or logical) partition.

Example 4

# dd if=/dev/hda | gzip > hda.img.gz

This command will do a copy of /dev/hda and piping it through the gzip compression program.

Add the Block Size and the number of blocks

With dd you can use also 2 important parameters :

bs= n Sets both input and output block sizes to n bytes.
count= n Copies only n input blocks.

Example 5

# dd if=/dev/zero of=/dev/null bs=1M count=32768
32768+0 records in
32768+0 records out
34359738368 bytes (34 GB) copied, 2.64483 s, 13.0 GB/s

This command give you a Processor/memory bandwidth in GB/s (in the example, a VPS on linode.com)

Example 6

#dd if=/dev/sda of=/home/sam/MBR.image bs=512 count=1

This command creates an image of the entire master boot record (including the partition table).

Example 7

#dd if=/dev/zero bs=1024 count=1000000 of=/home/sam/1Gb.file
 #dd if=/home/sam/1Gb.file bs=64k | dd of=/dev/null

These commands can be used to do a drive benchmark test and analyze the sequential read and write performance for 1024 byte blocks.

Seek and Skip

These 2 options are a bit less common but can be used to do particular tasks.

skip= n Skips n input blocks (using the specified input block size) before starting to copy. On seekable files, the implementation reads the blocks or seeks past them. On non-seekable files, the blocks are read and the data is discarded.

seek= n Skips n blocks (using the specified output block size) from beginning of output file before copying.

Example 8

#dd if=/dev/mem bs=1k skip=768 count=256 2>/dev/null | strings -n 8

This command display BIOS Information read the memory from C:0000 to F:FFFF without the need of dmidecode.

Example 9

#dd bs=1k if=image.nrg of=image.iso skip=300

This command converts a Nero Image File to ISO removing the 300k header from a Nero image file converting it to ISO format

Conclusions

This is just a small overview of the more common options for dd, there is also the option convert that permits to convert a lot of different file formats, perhaps it will be the topic of another article.



Popular Posts:

flattr this!

  11 Responses to “The dd command on Linux terminal.”

  1. There is a typo in example 4. Should be:
    dd if=/dev/hda | gzip > hda.img.gz

  2. There is also a basic graphical dd client written in C# and GTK available here, should you be interested.

    It doesn’t yet support setting block sizes or count, but they will be added eventually.
    http://rockcomputing.co.uk/projects.html

  3. Nice overview! Here’s another useful tip: to make dd output it’s progress, open up another terminal window and enter “killall -USR1 dd”. As with anything using kill or killall, make sure you type it correctly before hitting enter.

  4. You can verify that a disc burned properly by doing a bit by bit comparison using dd and cmp. You’ll want to unmount the disc first then run this command.

    dd if=[device file for your optical drive] | cmp [ISO file name ]

    example:

    dd if=/dev/sr0 | cmp my.iso

    I guess the same could be used for verifying files too.

  5. I have tried example #6 many times and it has never worked! I don’t get any errors, I just get another prompt. I never get a MBR.img file or anything else.

    • Check your output file destination. If you just want to create the file in your current directory, simply use of=MBR.image. You may also need to run the command as root. Some distro won’t report any privilege error.

  6. One kind hint
    I have two disk images.
    One is partial and covers the first 50% of a drive called “hdd A” (320GB), let’s call that partial image “disk-A-img1.bin”
    Next I have “disk-A-img2.bin”, which is 100% image of the same “hdd A” but contains many damaged sectors, especially in the first 50%.
    The goal I’d like to achieve is to use dd to overwrite the initial part of “disk-A-img2.bin” with the bytes contained in “disk-A-img1.bin”.
    Is it as simple as providing
    dd if=/tmp/disk-A-img2.bin of=/disk-A-img1.bin or are they required some more parameters?
    I ask this since I’m worrying about the file disk-A-img1.bin to be truncated at the size of disk-A-img2.bin, while as described above, disk-A-img1.bin is required to remain it original size 320GB.
    Thank you for hinting
    Frank

 Leave a Reply

(required)

(required)


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>