https://kerneltalks.com/commands/learn-dd-command-with-examples
Beginners guide to learn
Let me walk you through examples to understand
Backup complete disk using
For copying whole disk to another disk,
In above output you can see disk
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.
In above output, we created image of disk
Compressed image can be created as well using gzip along with
You can observe output zipped image is very much less in size.
Make sure your disk image and target disk has same size.
Restore compressed hard disk image using
Here, we specified 4096 block size using
Next question will be how to mount ISO file in Linux? Well we have already article on it here 🙂
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.
You can see all data in file is converted to uppercase.
For changing data from uppercase to lowercase use option
If you another interesting use of
Beginners guide to learn dd command along with list of examples. Article includes outputs for command examples too.
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 isddIt has very large list of options which can be used as per your requirement. Most of the commonly used options are :
1
2
3
4
5
6
|
bs=xxx Read and write xxx bytes at a time
count=n Copy only n blocks.
if=FILE Read from FILE
of=FILE Output to FILE
|
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 –
1
2
3
4
5
6
|
root@kerneltalks # dd if=/dev/xvdf of=/dev/xvdg
4194304+0 records in
4194304+0 records out
2147483648 bytes (2.1 GB) copied, 181.495 s, 11.8 MB/s
|
/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, thendd
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).
1
2
3
|
root@kerneltalks # dd if=/dev/xvdf of=/dev/null
|
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.
1
2
3
4
5
6
7
8
9
|
root@kerneltalks # dd if=/dev/xvdf of=/xvdf_disk.img
4194304+0 records in
4194304+0 records out
2147483648 bytes (2.1 GB) copied, 32.9723 s, 65.1 MB/s
root@kerneltalks # ls -lh /xvdf_disk.img
-rw-r--r--. 1 root root 2.0G Jan 15 14:36 /xvdf_disk.img
|
/dev/xvdf
into file located in /
named xvdf_disk.img
Compressed image can be created as well using gzip along with
dd
1
2
3
4
5
6
7
8
|
root@kerneltalks # dd if=/dev/xvdf |gzip -c >/xvdf_disk.img.gz
4194304+0 records in
4194304+0 records out
2147483648 bytes (2.1 GB) copied, 32.6262 s, 65.8 MB/s
root@kerneltalks # ls -lh /xvdf_disk.img.gz
-rw-r--r--. 1 root root 2.0M Jan 15 14:31 /xvdf_disk.img.gz
|
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.
1
2
3
4
5
6
|
root@kerneltalks # dd if=/xvdf_disk.img of=/dev/xvdg
4194304+0 records in
4194304+0 records out
2147483648 bytes (2.1 GB) copied, 175.748 s, 12.2 MB/s
|
Restore compressed hard disk image using
dd
along with gzip
command as below –
1
2
3
4
5
6
|
root@kerneltalks # gzip -dc /xvdf_disk.img.gz | dd of=/dev/xvdg
4194304+0 records in
4194304+0 records out
2147483648 bytes (2.1 GB) copied, 177.272 s, 12.1 MB/s
|
Create ISO from CD or DVD using dd
Another popular use ofdd
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.
1
2
3
|
root@kerneltalks # dd if=/dev/dvd of=/dvd_disc.iso bs=4096
|
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. Usingdd
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.
1
2
3
4
5
6
7
8
9
|
root@kerneltalks # dd if=/dev/zero of=/testfile bs=1M count=1024
1024+0 records in
1024+0 records out
1073741824 bytes (1.1 GB) copied, 13.0623 s, 82.2 MB/s
root@kerneltalks # ls -lh /testfile
-rw-r--r--. 1 root root 1.0G Jan 15 14:29 /testfile
|
Changing file uppercase to lowercase using dd
All above examples we seen so far are of data copy usingdd
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.
1
2
3
4
5
6
7
8
9
10
11
12
|
# cat /root/testdata
This is test data file on kerneltalks.com test server.
# dd if=/root/testdata of=/root/testdata_upper conv=ucase
0+1 records in
0+1 records out
55 bytes (55 B) copied, 0.000138394 s, 397 kB/s
# cat /root/testdata_upper
THIS IS TEST DATA FILE ON KERNELTALKS.COM TEST SERVER.
|
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