This tutorial explains Linux “dd” command, options and its usage with examples.
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.
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.:
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.
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:
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.
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.