TrumanWong

dd

Copy files and convert and format the contents of the original files

Supplementary instructions

dd command is used to copy files and convert and format the contents of the original files. The dd command is very powerful. For some low-level problems, using the dd command can often get unexpected results. The most commonly used method is to use dd to back up raw devices. However, it is not recommended. If you need to back up Oracle raw devices, you can use rman backup or third-party software backup. If you use dd, it will be inconvenient to manage.

It is recommended to use dd to operate the physical disk when necessary. If it is a file system, it is more convenient to use other commands such as tar backup cpio. In addition, when using dd to operate on the disk, it is best to use block device files.

grammar

dd(option)

Options

bs=<number of bytes>: Set ibs (input) and obs (output) to the specified number of bytes;
cbs=<number of bytes>: When converting, only the specified number of bytes will be converted each time;
conv=<keyword>: Specify the file conversion method;
count=<number of blocks>: only read the specified number of blocks;
ibs=<number of bytes>: the number of bytes read each time;
obs=<number of bytes>: the number of bytes output each time;
if=<file>: input file;
of=<file>: output to file;
seek=<Number of blocks>: Skip the specified number of blocks when output is started;
skip=<Number of blocks>: When starting to read, skip the specified number of blocks;
--help: help;
--version: Display version information.

Example

[root@localhost text]# dd if=/dev/zero of=sun.txt bs=1M count=1
1+0 records in
1+0 records out
1048576 bytes (1.0 MB) copied, 0.006107 seconds, 172 MB/s

[root@localhost text]# du -sh sun.txt
1.1M sun.txt

This command creates a 1M file sun.txt, in which the parameters are explained:

  • if represents the input file. If you do not specify if, input will be read from stdin by default.
  • of represents the output file. If of is not specified, stdout will be used as the default output by default.
  • bs represents the block size in bytes.
  • count represents the number of blocks copied.
  • /dev/zero is a character device and will continuously return 0 value bytes (\0).

Table of measurement units that can be used for block size

Unit Size Code
Byte (1B) c
Bytes (2B) w
block (512B) b
Kilobytes (1024B) k
Megabyte (1024KB) M
Gigabyte (1024MB) G

The above command can see the dd command to test the memory operation speed:

1048576 bytes (1.0 MB) copied, 0.006107 seconds, 172 MB/s

Generate random string

We can even use the /dev/urandom device with the dd command to get random strings.

[root@localhost ~]# dd if=/dev/urandom bs=1 count=15|base64 -w 0
15+0 records in
15+0 records out
15 bytes (15 B) copied, 0.000111993 s, 134 kB/s
wFRAnlkXeBXmWs1MyGEs

Test disk write speed

[root@localhost ~]# dd if=/dev/zero of=/tmp/testfile bs=1G count=1 oflag=direct
1+0 records in
1+0 records out
1073741824 bytes (1.1 GB) copied, 7.10845 s, 151 MB/s

Test disk read speed

[root@localhost ~]# dd if=/tmp/testfile of=/dev/null bs=1G count=1 iflag=direct
1+0 records in
1+0 records out
1073741824 bytes (1.1 GB) copied, 6.53009 s, 164 MB/s