dd Command in Linux with Examples

This tutorial explains Linux “dd” command, options and its usage with examples.

“dd” command is used to convert and copy a file. This post describes “dd” command used in Linux along with usage examples and/or output.

Usage:
dd [OPERAND]..

On Unix, device drivers for hardware (such as hard disks) and special device files (such as /dev/zero and /dev/random) appear in the file system just like normal files; dd can also read from (and in some cases write to) these files. As a result, dd can be used for tasks such as backing up the boot sector of a hard drive, and obtaining fixed amount of random data.
Only superuser can execute “dd” command.

NOTE
While executing dd command you need to be very careful, otherwise you may lose all your data which will become difficult to recover again.

In all “dd” command:
* “if” stands for input file
* “of” stands for output file.
* while execution if any error happens then command fails.
* if you do not take care while mentioning the name of output file and mentions a name of preexisting file, it may get overwritten and eventually cause a data loss.

Here’s the listing of example usage of “dd” command.:

advertisement
advertisement

1. To Create an Image of a Hard Disk:
You can take a image file of hard disk as a backup with “dd” command. This is a very easy and fastest way of getting a backup of a entire hard disk.

root@sanfoundry->dd if=/dev/hda of=~/disk.img

This command takes images of /dev/hda into a image file named disk.img.

2. To Restore data using image of hard disk:
All the data of hard disk can be restored with the help of image created.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
root@sanfoundry-> dd if=disk.img of=/dev/hdb

3. To Copy the contents from one drive to another:

root@sanfoundry-> dd if=/dev/sda of=/dev/sdb

Here contents of hard drive /dev/sda are copied to drive /dev/sdb.So with this command you can take a backup of a entire hard disk.

4. To create an ISO image file named image.iso of a CD:

advertisement
root@sanfoundry-> dd if=/dev/cdrom of=create.iso bs=3k

Here create.iso is a backup file created for /dev/cdrom of bytes=3k.Here “bs” stands for a block size. so here dd command reads a block of 3000 bytes at a time from input file and copies the same to the output file.But before doing this you have to take care of not giving a random access to the mounted filesystem. So If CD is auto mounted, before creating an iso image using dd command, its always good if you unmount the CD device.

5. To Create a Floppy Image:

root@sanfoundry-> dd if=/dev/fd0 of=floppy.img

Here image of floppy /dev/fd0 is created as floppy.img.

advertisement

6. To create a file, filled with random data:

root@sanfoundry-> dd if=/dev/random of=random.txt count=2M

Here all the random data from /dev/random is going into random.txt file with count=2M showing “copy only 2M input blocks”. But a more faster way to do this is to use /dev/urandom :

root@sanfoundry-> dd if=/dev/urandom of=random.txt count=2M

Sanfoundry Global Education & Learning Series – 1000 Linux Tutorials.

If you wish to look at all Linux commands and their usage examples, go to Linux Commands Tutorial.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.