Learn Linux DD Command - 15 Examples With All Options

June 24, 2011 | By
| 4 Replies More

The Linux command ‘dd’ is one of the most powerful utility which can be used in a variety of ways. This tool is mainly used for copying and converting data, hence it stands for ‘data duplicator’.

This tool can be used for:

• Backing up and restoring an entire hard drive or a partition.

• Copy regions of raw device files like backing up MBR (master boot record).

• Converting data formats like ASCII to EBCDIC.

• Converting lowercase to uppercase and vice versa.

• Creating files with fixed size.

Only superuser can execute this command. You should be very careful while using this command as improper usage may cause huge data loss. So, some people consider this tool as ‘data destroyer’.

Syntax of ‘dd’ command.

dd  if=<source file name> of=<target file name> [Options]

We will learn the various ‘options’ while going through the examples.

1. Backing up and restoring an entire hard drive or a partition.

a. Backup entire hard drive to another drive.

dd if=/dev/sda of=/dev/sdb bs=4096 conv=noerror,sync

Here, ‘if’ stands for input file , ‘of’ stands for output file and ‘bs’ stands for the block size (number of bytes to be read/write at a time). The conversion parameter ‘noerror’ allows the tool to continue to copy the data even though it encounter any errors. The sync option allows to use synchronized I/O.

The above command will copy all the data from the disk /dev/sda to /dev/sdb. ‘dd’ doesn’t know anything about the filesystem or partitions; it will just copy everything from /dev/sda to /dev/sdb. So, this will clone the disk with the same data on same partition.

b. Creating a disk image.

dd if=/dev/sda of=/tmp/sdadisk.img

Backing up a disk to an image will be faster than copying the exact data. Also, disk image make the restoration much more easier.

c. Creating a compressed disk image.

dd if=/dev/sda | gzip >/tmp/sdadisk.img.gz

d. Restoring hard disk image.

dd if=/tmp/sdadisk.img of=/dev/sda

e. Restoring compressed image.

gzip –dc /tmp/sdadisk.img.gz | dd of=/dev/sda

f. Clone one partition to another.

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

This will synchronize the partition /dev/sda1 to /dev/sdb1. You must verify that the size of /dev/sdb1 should be larger than /dev/sda1

2. Backing up and restoring MBR.

Master Boot record is the boot sector which houses the GRUB boot loader. If MBR gets corrupted, we will not be able to boot into Linux. MBR -512 byte data- is located at the first sector of the hard disk. It consists of 446 byte bootstrap, 64 byte partition table and 2 bytes signature.

a. Backing up MBR.

dd if=/dev/sda of=/tmp/mbr.img bs=512 count=1

The option “count” refers to the number of input blocks to be copied.

b. Backing up the boot data of MBR excluding the partition table.

dd if=/dev/sda of=/tmp/mbr.img bs=446 count=1

c. Restoring MBR from MBR image.

dd if=/tmp/mbr.img of=/dev/sda

d. Display master boot record.

dd if=/dev/hda of=mbr.bin bs=512 count=1
od -xa mbr.bin

3. Converting data formats.

a. Convert the data format of a file from ASCII to EBCDIC.

dd if=textfile.ascii of=textfile.ebcdic conv=ebcdic

b. Convert the data format of a file from EBCDIC to ASCII

dd if=textfile.ebcdic of=textfile.ascii conv=ascii

4. Converting case of a file.

a. Converting a file to uppercase.

dd if=file1 of=file2 conv=ucase

b. Converting a file to lowercase.

dd if=file1 of=file2 conv=lcase

5. Creating or modifying data files.

a. Create a fixed size, say 10MB file.

dd if=/dev/zero of=file1 bs=10485760 count=1

The block size is calculated as 10MB=10*1024*1024.

b. Modify the first 512 bytes of a file with null data.

dd if=/dev/zero of=file1 bs=512 count=1 conv=notrunc

The option ‘notrunc’ refers to do not truncate the file, only replace the first 512 bytes, if it exists. Otherwise, you will get a 512 byte file.


These are the some examples of ‘dd’ command usage. This ‘data duplicator’ command can be used in a lot more ways in your daily administration tasks.


Tagged With :

Free Linux Ebook to Download

Comments (4)

Trackback URL | Comments RSS Feed

  1. Pawan says:

    Thanks very nice explanation of DD command. I love making files for SWAP with this command.

  2. HyunWoo Jo says:

    Thank you for good example.


    I have gained a lot.

  4. Roderick says:

    Sorry if I ask a dumb question, but will the below command
    dd if=/tmp/mbr.img of=/dev/sda
    not destroy the /dev/sda, leaving only the MBR?
    You see, if I have, say, a file 1.txt with '123456789' in its contents and I run
    dd if=/dev/zero of=1.txt bs=1 count=1
    it will put 1 byte into this file, wiping out everything else.
    To only replace the first byte, AFAIK, I have to use conv=notrunc parameter, i.e.
    dd if=/tmp/mbr.img of=/dev/sda conv=notrunc
    Then only the first byte is change and everything else stays intact.

Leave a Reply

Commenting Policy:
Promotion of your products ? Comment gets deleted.
All comments are subject to moderation.