Linux DD Command : 15 Examples With All Options (backup master boot record / MBR)

The Linux command ‘dd’ is one of the very 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 eventhough 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 got 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.


Posted on June 17, 2015, in Linux (Ubuntu/CentOS). Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: