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.
In above output, we created image of disk /dev/xvdf into file located in / named xvdf_disk.img Compressed image can be created as well using gzip along with 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.
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.
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.
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.
No comments:
Post a Comment