Linux DD Command
Linux DD Command
Linux DD Command
The Linux dd command 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. This tool can be used for:
Only superuser 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.
Syntax of dd command
The basic use of the dd command is rather easy because it takes just two
arguments: if= to specify the input file and of= to specify the output file. The
arguments to those options can be either files or block devices. I would, however,
not recommend using dd to copy files because cp does that in a much simpler way.
However, you can use it to clone a hard disk. The syntax is
ddif=<sourcefilename>of=<targetfilename>[Options]
We will learn the various options while going through the examples.
1. Backing up and restoring an entire
disk or a partition
It is possible to save all the data from an entire disk/partition to another disk/partition.
Not a simple copy as cp command but a block size copy.
#ddif=/dev/sdaof=/dev/sdbbs=4096conv=noerror,sync
97281+0recordsin
97280+0recordsout
99614720bytes(100MB)copied,2.75838s,36.1MB/s
This works only if the second device is as large as or larger than the first.
Otherwise, you get truncated and worthless partitions on the second one. Here, if
stands for input file , of stands for output file and bs stands for the block size
(number of bytes to be read/write at a time). Make sure you use block sizes in
multiples of 1024 bytes which is equal to 1KB. If you don't specify block size, dd use
a default block size of 512 bytes. The conv value parameter noerror allows the
tool to continue to copy the data even though it encounters any errors. The sync
option allows to use synchronized I/O.
You can store the output file where you want but you have to give a filename ending
with .img extension as above. Instead of /tmp/sdadisk.img , you could store
it for example at /sdadisk.img if you want.
#ddif=/dev/vda|gzip
c>/tmp/vdadisk.img.gz
The pipe | operator makes the output on the left command become the input on the
right command. The -c option writes output on standard output and keeps original
files unchanged.
#ddif=/dev/sda1of=/dev/sdb1bs=4096conv=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 . Or you can
create a partition image as below
#ddif=/dev/sda1of=/tmp/sda1.img
#ddif=/tmp/sdadisk.imgof=/dev/sda
You will retrieve data which were presents before the backup operation and not after
the operation
#gzip
dc/tmp/vdadisk.img.gz|ddof=/dev/vda
The -d option here is to uncompress. Note the output file. You can mount the
restored disk to see the content. Note that you will data added after the last
compression backup operation.
#ddif=/dev/zeroof=/filebs=1024Kcount=500
500+0recordsin
500+0recordsout
524288000bytes(524MB)copied,1.21755s,431MB/s
The option count refers to the number of input blocks to be copied. Combined
with block size value, it indicates the total size to copy. For example bs=1024k
and count=500 give a size=1024K*500 =524288000 bytes =524MB
#ls
lh/file
rw
rr
1rootroot500MMay1718:57/file
You can see that we have our virtual filesystem created with the size indicated. You
can now use it to create loop device or a virtual disk or anything else.
The notrunc option refers to do not truncate the file, only replace the first 512
bytes, if it exists. Otherwise, you will get a 512 byte file
#ddif=/dev/cdromof=/mycd.iso
You need to know that you have to use the -o loop option, which allows you to
mount a file like any normal device. So, to mount /mycd.iso on the /mnt/cd
directory, do as below
#mount
oloop/mycd.iso/mnt/cd
a. Backing up MBR
Because the MBR makes up the first 512 bytes of the disk, we just need to copy that
block size
#ddif=/dev/sdaof=/tmp/sdambr.imgbs=512count=1
With the count=1 and bs=512 , only 512 bytes will be copied which correspond
to the size of our MBR.
You can display the saved MBR with the od command which dump files in octal and
other formats as below
#od
xa/tmp/sdambr.img
0000000bf5281f48b66832d087d0f00e284
8000
R?tsohfvt
etx}bsnulsieotbnu
lnul
0000020ff7c740066461d8b8b66044d3166
b0c0
|delnultFfvtgsfvtMeotf1
@0
#ddif=/tmp/sdambr.imgof=/dev/sda
#ddif=textfile.ebcdicof=textfile.asciiconv=ascii
The conv value parameter now is ascii because we convert from EBCDIC to
ASCII
#ddif=textfile.asciiof=textfile.ebcdicconv=ebcdic
The conv value parameter now is ebcdic because we convert from ASCII to
EBCDIC. If youre just replacing particular number of bytes with an equivalent
number of bytes having different characters, the conversion would be smooth and
application reading the file should not have any issues.
4. Converting case of a le
dd command can be also used for an amazing thing. It can convert all text
(alphabets) in a file to upper or lower case and vice versa. For the example below,
we will have a file for the tests.
#catfile10
testddconvert
a. Converting a le to uppercase
Because our text file example is on lowercase, we will convert it to uppercase
#ddif=~/file10of=~/file20conv=ucase
The command will create the new file indicated. See that now conv option takes
ucase value. Let's check the result
After modifying the ASCII version and once done, you may convert it back to
EBCDIC to be used by your application.
#ddif=textfile.asciiof=textfile.ebcdicconv=ebcdic
The conv value parameter now is ebcdic because we convert from ASCII to
EBCDIC. If youre just replacing particular number of bytes with an equivalent
number of bytes having different characters, the conversion would be smooth and
application reading the file should not have any issues.
4. Converting case of a le
dd command can be also used for an amazing thing. It can convert all text
(alphabets) in a file to upper or lower case and vice versa. For the example below,
we will have a file for the tests.
#catfile10
testddconvert
a. Converting a le to uppercase
Because our text file example is on lowercase, we will convert it to uppercase
#ddif=~/file10of=~/file20conv=ucase
The command will create the new file indicated. See that now conv option takes
ucase value. Let's check the result
#catfile20
TESTDDCONVERT
b. Converting a le to lowercase
Now we will do the reverse operation which will convert to lowercase
#ddif=~/file20of=~/file30conv=lcase
See that we use lcase of conv option to convert from upper case to lower case.
#catfile30
testddconvert
dd command does not convert the file names, only its content.
Conclusion
These are some examples of dd command usage. This data duplicator command
can be used in a lot more ways in your daily administration tasks. The dd command,
although not technically an archiving command, is similar in some ways because it
can copy an entire partition or disk into a file and vice versa.