Learn to Use a DD (Data Duplicator) Command in Linux
In this article we are knowing about the one most important utility tool of Linux operating system.The Linux command ‘dd’ is one of the most 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’.
The Various Purpose of this tool are:
- 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.
# dd if=<source file name> of=<target file name> [Options]
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
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 gets 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=1od -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.
This ‘data duplicator’ command can be used in a lot more ways in your daily administration tasks.