Data loss will be costly. At the very least, critical data loss will have a financial impact on companies of all sizes. In some cases, it can cost your job. I’ve seen cases where sysadmins learned this in the hard way.

The dd command stands for “data duplicator” and used for copying and converting data. It is very powerful low level utility of Linux which can do much more like;

• Backup and restore the entire hard disk or partition.
• Backup of MBR (Master Boot Record)
• It can copy and convert magnetic tape format, convert between ASCII and EBCDIC formats, swap bytes  and can also convert lower case to upper case.
• It can also be used by Linux kernel make files to make boot images.


Syntax of dd command

Before we start with some practical work we need to talk about its syntax.

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

We normally do not explain about syntax but this command syntax require some explanation. The syntax is totally different when compared to many Linux commands we know. In this syntax dd is followed by two things

if=<source> – This is a source from where you want to copy data and ‘if’ stands for input-file.

of=<destination> – This is a source from where you want to write/paste data and ‘of’ stands for output-file.

[options] – These options include, how fast data should be written, what format etc.

Input(source file name) and Output(target file name) in syntax are disks, partitions, files and devices to which you want to write and read data from. There are many options which we will discuss in examples.

Learn Linux dd command with examples

Warning: Only superuser can run this command because you can face a big data loss due to its improper usage, so you should be very careful while working with this utility. At that moment data loss can convert the dd utility as a “data destroyer” for you.

Example 1. Backup Entire Harddisk

To backup an entire copy of a hard disk to another hard disk connected to the same system, execute the dd command as shown below. In this dd command example, the UNIX device name of the source hard disk is /dev/hda, and device name of the target hard disk is /dev/hdb.

# dd if=/dev/sda of=/dev/sdb
  • “if” represents inputfile, and “of” represents output file. So the exact copy of /dev/sda will be available in /dev/sdb.
  • If there are any errors, the above command will fail. If you give the parameter “conv=noerror” then it will continue to copy if there are read errors.
  • Input file and output file should be mentioned very carefully, if you mention source device in the target and vice versa, you might loss all your data.

In the copy of hard drive to hard drive using dd command given below, sync option allows you to copy everything using synchronized I/O.

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

Example 2. Create an Image of a Hard Disk

Instead of taking a backup of the hard disk, you can create an image file of the hard disk and save it in other storage devices.There are many advantages to backing up your data to a disk image, one being the ease of use. This method is typically faster than other types of backups, enabling you to quickly restore data following an unexpected catastrophe.

# dd if=/dev/hda of=~/hdadisk.img

The above creates the image of a harddisk /dev/hda. Do you feel hdadisk.img is bit big? Use gzip or bzip2 to compress when creating image.

dd if =/dev/sda2 | bzip2 hdadisk.img.bz2

Example 3. Restore using Hard Disk Image

To restore a hard disk with the image file of an another hard disk, use the following dd command example.

# dd if=hdadisk.img of=/dev/hdb

The image file hdadisk.img file, is the image of a /dev/hda, so the above command will restore the image of /dev/hda to /dev/hdb.

Example 4. Creating a Floppy Image

Using dd command, you can create a copy of the floppy image very quickly. In input file, give the floppy device location, and in the output file, give the name of your floppy image file as shown below.

# dd if=/dev/fd0 of=myfloppy.img

Example 5. Backup a Partition

You can use the device name of a partition in the input file, and in the output either you can specify your target path or image file as shown in the dd command example below.

# dd if=/dev/hda1 of=~/partition1.img

Example 6. CDROM Backup

dd command allows you to create an iso file from a source file. So we can insert the CD and enter dd command to create an iso file of a CD content.

# dd if=/dev/cdrom of=tgsservice.iso bs=2048

dd command reads one block of input and process it and writes it into an output file. You can specify the block size for input and output file. In the above dd command example, the parameter “bs” specifies the block size for the both the input and output file. So dd uses 2048bytes as a block size in the above command.

Note: If CD is auto mounted, before creating an iso image using dd command, its always good if you unmount the CD device to avoid any unnecessary access to the CD ROM.

Example 7: Wipe/delete content of a disk

Erase entire disk, so that it will be empty for someone to use it

dd if=/dev/zero of=/dev/sdb

This will wipe out your second hard disk and every bit is written with zero.

How writing of a file  on hard disk with data happen?

Normally what ever you write on to a disk at the block level it will write combination of zeros and ones. Hope you know this and what we are doing here is that we are just writing zeros which will clear all 1’s from the hard disk. This eventually makes your disk empty.

Example 8: Quick personal data deleting

Example 7 shows you a complete way to erase your disk, but takes time, you want quick and safe delete your personal data ? Many people think if we do rm -rf /<your data> will do the needful. But we can recover those deletion by using disk recovery tools like Photorec or some forensic tools

Here is another way if you want some not to recover your data you have to write random data on your partition where you data resides.

dd if=/dev/random of=/dev/sdb

Do above command multiple times so that it is real hard to recover data. If I am in your place, I will write below shell script to do that.

for i in {1..10};do dd if=/dev/random of=/dev/sdb;done

This will execute dd command 10 times in a row one after the other.

Example 9:  Create a certain size empty file

For whatever reason, the following dd command creates a huge file which eats up HDD in no time.

dd if=/dev/zero of=/usr/disk-img/disk-quota.ext3 count=40960 

This will create 20MB file (disk image) at said path.

Example 10: Create a bootable USB

We can even create bootable USB’s using dd command.

dd if=/home/$user/bootimage.img of=/dev/sdc

This will create boot-able USB drive where /dev/sdc is an USB drive.


Comments powered by CComment