The dd command stands for "data duplicator" or "data destroyer" based on its usage. It is very powerful low level utility of Linux that can be used in variety of ways.
Backing up and restoring an entire hard drive or a partition
Creating virtual filesystem and backup images of CD or DVDs called ISO files
Copy regions of raw device files like backing up MBR (Master Boot Record)
Converting data formats
Converting File from lowercase to uppercase and vice versa
More information can be found on its man page.
root:~# man dd
Only superuser can execute this command.
Warning! User should be very careful while using this command as improper usage may cause data loss so that is another reason in linux community its famous as data destroyer.
The basic use of the dd command is rather easy because it takes just two arguments: if= to specify the input file and of= to specify the output file.
This command will copy all the data (Entire disk) from the /dev/sda to /dev/sdb. dd doesn't know anything about the filesystem or partitions; it will just copy everything from input to output device. You need to indicate the block size to be copied at time with bs option. So, this will clone the disk with the same data on the same partition. This works only if the second device(of=) is as large than the first; otherwise user gets truncated and worthless partitions on the second one.
if= stands for input file.
of= stands for output file.
bs= stands for block size. Make sure its in multiples of 1024 (1KB). If user doesn't provide bs then default size of 512bytes will be considered.
conv= value parameter noerror allows the tool to continue to copy the data even though it encounters any errors.The sync option allows to use Synchronized I/O.
1.2 Create Disk Image (File Image)
The pipe (|) operator makes the output of left command become the input of the right command. The -c option writes output on standard output and keeps original file unchanged.
1.5 Restore Compressed Disk Image
2.1 Create a Virtual File System
A virtual filesystem is a kind of file that need it to create for example an additional swap or loop device or a virtual machine./dev/zero file is needed to create a virtual file.
root:~# dd if=/dev/zero of=AllZero bs=1024K count=500
500+0 records in
500+0 records out
524288000 bytes (524 MB) copied, 0.155236 s, 3.4 GB/s
2.2 Modify the first 1024 bytes of a file with null data
notrunc option refers to dont truncate the file, only replace the first 1024 bytes if file exists. Otherwise, user gets a 1024 byte file.
2.3 Creating a backup iso image of CDROM
User might be wondering why not just copy the contents of CD/DVD to a directory. How would you handle the boot sector of it? User can't find that as a file on the device because its just the first sector. As dd copies sector by sector, it will copy this information as well
root:~# dd if=/dev/cdrom of=/YOUR_DIR/MyCD.iso
Note:Before creating an ISO image if CD is auto mounted then it's always good to unmount the CD device to avoid any unnecessary access to the CD ROM.
This iso image can be mount like a file by using following command.
root:~# dd -o loop if=/YOUR_DIR/MyCD.iso /mnt/cd
2.4 Creating a backup of Floppy Disk
root:~# dd if=/dev/fd0 of=/YOUR_DIR/MyFloppy.img
Example-3
3.1 Converting data formats of a File from EBCDIC to ASCII
4.1 Converting case of a file - From Lower case to Upper case
root:~# cat lowercasetext
Hello world.
How are you?
root:~# dd if=lowercasetext of=uppercasetext conv=ucase
0+1 records in
0+1 records out
26 bytes (26 B) copied, 3.4099e-05 s, 762 kB/s
root:~# cat uppercasetext
HELLO WORLD.
HOW ARE YOU?
4.2 Converting case of a file - From Upper case to Lower case
root:~# cat uppercasetext
HELLO WORLD.
HOW ARE YOU?
root:~# dd if=uppercasetext of=lowercasefile conv=lcase
0+1 records in
0+1 records out
26 bytes (26 B) copied, 3.5173e-05 s, 739 kB/s
root:~# cat lowercasefile
hello world.
how are you?