5 Useful Unix DD Command Examples

On September 29, 2010, in Linux, Unix Tools, by erik

dd is a common Unix program whose primary purpose is the low-level copying and conversion of raw data. You can backup whole hard drives, create a large file filled with only zeros, create and modify image files at specific points, and even do conversions to upper case.

To display dd‘s help simply enter:

erik@debian:~$dd --help

Alright, lets get to the juicy stuff.

1. Make an ISO of a your favourite CD just for backing up purposes with dd:

dd if=/dev/cdrom of=/home/erik/myCD.iso bs=2048 conv=sync

Breaking down the commands:

  • if is “input file”, so in this case our cdrom drive at /dev/cdrom
  • of is “output file”, in this case myCD.iso
  • bs is “block size”, in this case 2048 bytes per block
  • conv is for conversion, in this case we are using “sync” which tells DD to execute synchronized input and output, this is needed for the CD-ROM as we want to read a whole block to ensure no data loss occurs.

2. Duplicate one hard disk partition to another hard disk with dd:

dd if=/dev/sda1 of=/dev/sdb1 bs=4096 conv=noerror

In this case everything is the same as example 1 but our conversion methods states that noerror should be executed, this tells DD to continue after read errors.

3. Fill a file with 1MB of random bytes with dd:

erik@debian:~$dd if=/dev/urandom bs=1024 count=1000 of=fun.bin
1000+0 records in
1000+0 records out
1024000 bytes (1.0 MB) copied, 0.198349 s, 5.2 MB/s

This time I stated that our block size is 1024 bytes, and we are going to make 1000 of them sequentially. I also used the built-in kernel device urandom which provides random bytes.

4. Skip first 128K of input file then write remaining with dd:

dd if=/home/erik/fun.bin skip=128k bs=1 of=/home/erik/fun2.bin

The skip command tells DD to move passed the (in this case) 128k of data in fun.bin then write the rest to fun2.bin. This can be handy if you have a large file that needs to be written across more than one partition. For instance, if you had 3 partitions each 128k. You wouldn’t want to write the same 128k to each partition, you would want to write the first 128k to partition 1, then from 128k-256k of the file to partition 2 and so on.

5. Using dd to convert a file to uppercase:

dd if=erik.txt of=erik_up.txt conv=ucase

Finally, we use conv again to do a conversion. In this case we convert with the specifier of ucase.

What is your favourite use of dd?

One Response to 5 Useful Unix DD Command Examples

  1. james says:

    Flag symbols
    while looking at a linux distro bash 4.2.8.
    the dd --help has these words: Each flag symbol may be: and then a list.
    i do not understand this part ‘ Each flag symbol ‘ what is the flag symbol. the author here also uses the word options at the bottom of the page, so i do assume the author is not mixing descriptive definitive words in using flag in place of option.
    yet in http://en.wikipedia.org/wiki/Command-line_interface#Command-line_option there is this to munch on: A command-line option or simply option (also known as a flag or switch) modifies the operation of a command; the effect is determined by the command’s program. Options follow the command name on the command line, separated by spaces. A space before the first option is not always required.
    how to really understand and define flags could be the subject header.

Leave a Reply

Your email address will not be published. Required fields are marked *

+ six = 13

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>