Thursday, January 18, 2018

Learn dd command with examples

https://kerneltalks.com/commands/learn-dd-command-with-examples

Beginners guide to learn dd command along with list of examples. Article includes outputs for command examples too.

Learn dd command
Beginners guide to learn dd command! In this article we will learn about dd (Disk Duplication) command and various usage of it along with examples.dd command mainly used to convert and copy files in Linux and Unix systems. dd command syntax is

dd

It has very large list of options which can be used as per your requirement. Most of the commonly used options are :

Shell

1

2

3

4

5

6

bs=xxx Readandwritexxx bytes atatime

count=nCopy onlynblocks.

if=FILEReadfrom FILE

of=FILEOutput toFILE

Let me walk you through examples to understand dd command usage.

Backup complete disk using dd

For copying whole disk to another disk, dd is very helpful. You just need to give it disk to read from and disk to write. Check below example –

Shell

1

2

3

4

5

6

root@kerneltalks# dd if=/dev/xvdf of=/dev/xvdg

4194304+0records in

4194304+0records out

2147483648bytes(2.1GB)copied,181.495s,11.8MB/s

In above output you can see disk /dev/xvdf is copied to /dev/xvdg. Command will show you how much data and what speed it copied.

Identify disk physically using dd

When there are bunch of disks attached to server and if you want to trace particular disk physically, then dd
command might be helpful. You have to run dd command to red from disk
and write into void. This will keep the hard disk activity light solid
(physical on disk).

Shell

1

2

3

root@kerneltalks# dd if=/dev/xvdf of=/dev/null

Normally
all other disk blinking activity LED wheras this one will be having its
LED solid. Easy to spot the disk then! Be careful with IF and OF. IF
you switch their arguments, you will end up wiping out your hard disk
clean.

Create image of hard disk using dd

You can create image of hard disk using dd.
Its same as what we seen in first example backup of disk. Here we will
use output file OF as a data file on mount point and not the another
disk.

Shell

1

2

3

4

5

6

7

8

9

root@kerneltalks# dd if=/dev/xvdf of=/xvdf_disk.img

4194304+0records in

4194304+0records out

2147483648bytes(2.1GB)copied,32.9723s,65.1MB/s

root@kerneltalks# ls -lh /xvdf_disk.img

-rw-r--r--.1root root2.0GJan1514:36/xvdf_disk.img

In above output, we created image of disk /dev/xvdf into file located in / named xvdf_disk.imgCompressed image can be created as well using gzip along with dd

Shell

1

2

3

4

5

6

7

8

root@kerneltalks# dd if=/dev/xvdf |gzip -c >/xvdf_disk.img.gz

4194304+0records in

4194304+0records out

2147483648bytes(2.1GB)copied,32.6262s,65.8MB/s

root@kerneltalks# ls -lh /xvdf_disk.img.gz

-rw-r--r--.1root root2.0MJan1514:31/xvdf_disk.img.gz

You can observe output zipped image is very much less in size.

Restore image of hard disk using dd

Yup,
next question will be how to restore this hard disk image on another
disk? Answer is simple use it as source and destination as another disk.

Shell

1

2

3

4

5

6

root@kerneltalks# dd if=/xvdf_disk.img of=/dev/xvdg

4194304+0records in

4194304+0records out

2147483648bytes(2.1GB)copied,175.748s,12.2MB/s

Make sure your disk image and target disk has same size.Restore compressed hard disk image using dd along with gzip command as below –

Shell

1

2

3

4

5

6

root@kerneltalks# gzip -dc /xvdf_disk.img.gz | dd of=/dev/xvdg

4194304+0records in

4194304+0records out

2147483648bytes(2.1GB)copied,177.272s,12.1MB/s

Create ISO from CD or DVD using dd

Another popular use of dd
command is creating optical disk image file i.e. ISO file from CD or
DVD. You need to first mount CD or DVD on your server then use it as a
source device and file on mount point as a destination.

Shell

1

2

3

root@kerneltalks# dd if=/dev/dvd of=/dvd_disc.iso bs=4096

Here, we specified 4096 block size using bs option. Make sure no other application or user is accessing CD or DVD when running this command. You can use fuser command to check if someone is accessing it.
Next question will be how to mount ISO file in Linux? Well we have already article on it here 🙂

Creating file of definite size with zero data using dd

Many times sysadmins or developers needs files with junk data or zero data for testing. Using dd you can create such files with definite size.
Lets say you want to create file of 1GB then you define block size of 1M and count of 1024. So 1M x 1024 = 1024M = 1G.

Shell

1

2

3

4

5

6

7

8

9

root@kerneltalks# dd if=/dev/zero of=/testfile bs=1M count=1024

1024+0records in

1024+0records out

1073741824bytes(1.1GB)copied,13.0623s,82.2MB/s

root@kerneltalks# ls -lh /testfile

-rw-r--r--.1root root1.0GJan1514:29/testfile

In above output you can see our math worked perfectly. 1G file is created out of our command.

Changing file uppercase to lowercase using dd

All above examples we seen so far are of data copy using dd command. Now this example is of data convert using dd command. Using dd, you can change file data from all uppercase to lowercase and vice versa.

Shell

1

2

3

4

5

6

7

8

9

10

11

12

# cat /root/testdata

Thisistestdata fileon kerneltalks.comtestserver.

# dd if=/root/testdata of=/root/testdata_upper conv=ucase

0+1records in

0+1records out

55bytes(55B)copied,0.000138394s,397kB/s

# cat /root/testdata_upper

THISISTESTDATA FILEON KERNELTALKS.COMTESTSERVER.

You can see all data in file is converted to uppercase.
For changing data from uppercase to lowercase use option conv=lcase
If you another interesting use of dd command, let us know in comments down below.