Pages

Saturday, December 12, 2015

dd command one liners


The Linux command ‘dd’ is one of the very powerful utility which can be used for copying and converting data, hence it stands for ‘data duplicator’. Only superuser root can execute this command.
You should be very careful while using this command as improper usage may cause huge data loss. So, some people consider this tool as ‘data destroyer’.

This tool can be used for.
• Backing up and restoring an entire hard drive or a partition
• Creating files with fixed size
• Copy regions of raw device files like backing up MBR(master boot record)

Syntax of ‘dd’ command
dd  if=<source file name> of=<target file name> [Options]


1.       Backup entire hard drive to another drive.

dd if=/dev/sda of=/dev/sdb bs=4096 conv=noerror

Where,
if – for input file
of – for output file
bs – for the block size i.e. number of bytes to be read/write at a time
conv – The conversion parameter ‘noerror’ allows the tool to continue to copy the data even though it encounter any errors.
The above command will copy all the data from the disk /dev/sda to /dev/sdb. Command 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.

2.       Creating a disk image

dd if=/dev/sda of=/tmp/sdadisk.img

3.       Creating a compressed disk image

dd if=/dev/sda | gzip >/tmp/sdadisk.img.gz

4.       Restoring hard disk image

dd if=/tmp/sdadisk.img of=/dev/sda

5.       Restoring compressed image
gzip -dc /tmp/sdadisk.img.gz | dd of=/dev/sda

6.       Backing up and Restoring MBR
Master Boot record is the boot sector which houses the GRUB boot loader. If MBR got 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.

Backing up MBR
dd if=/dev/sda of=/tmp/mbr.img bs=512 count=1
Note: The option “count” refers to the number of input blocks to be copied

Backing up the boot data of MBR excluding the partition table
dd if=/dev/sda of=/tmp/mbr.img bs=446 count=1

Restoring MBR from MBR image
dd if=/tmp/mbr.img of=/dev/sda

7.       Converting data formats

Convert the data format of a file from ASCII to EBCDIC
dd if=textfile.ascii of=textfile.ebcdic conv=ebcdic

Convert the data format of a file from EBCDIC to ASCII
dd if=textfile.ebcdic of=textfile.ascii conv=ascii

8.       Converting case of a file

Converting a file to Uppercase
dd if=file1 of=file2 conv=ucase

Converting a file to lowercase
dd if=file1 of=file2 conv=lcase

9.       Creating or modifying data files

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

Modify the first 512 bytes of a file with null data
dd if=/dev/zero of=file1 bs=512 count=1 conv=notrunc
Where,

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.

No comments:

Post a Comment