Detailed explanation of inux DD command

Time:2021-10-27

The main job of Linux operating system administrator is to ensure that the Linux system can run normally during work. However, it is not so easy to do so, because the operating environment of Linux system is very complex, and there will be many factors leading to the collapse of Linux system. Once the system crashes, Linux system administrators need to recover the Linux system in the fastest time and do not lose data to the greatest extent, which is a great challenge for system administrators. Next, Liang Xu Xiaobian will elaborate on the * * Linux DD command from several aspects, hoping to help you.

Linux命令

To achieve this goal, the only way we can do is to back up and re back up the data. The DD command line utility is a very frequently used tool for system backup and recovery on Linux system.

DD is a command line utility on UNIX and Linux operating systems. Its main purpose is to convert and copy files. Because the contents of a hard disk boot sector have been generated before installing the file system, it cannot be backed up by the copy command of the operating system. At this time, we need to use DD program to complete the backup of the boot sector of the hard disk, or use DD command to completely clear the data on a hard disk, and complete the data conversion when copying data.

The name of DD comes from the DD statement in IBM’s job control language (JCL). DD represents the first letter of the word data definition. Therefore, unlike the syntax format of most other UNIX commands, the DD command is more like an IBM JCL statement than a UNIX command.

Many options can be used in the DD command. The syntax format of these options is as follows:

Option = option value

Space separation is required between each option.

Table 1 common options of DD command

Options meaning
If = file name Enter the file name. The default is standard input, that is, specify the source file, such as < if = input file >.
Of = file name Output file name. The default is standard output, that is, specify the destination file, such as < of = output file >.
ibs=bytes obs=bytes bs=bytes Read in bytes at a time, that is, specify a block size of bytes. Output bytes at a time, that is, specify a block size of bytes. At the same time, set the read in / output block size to bytes.
cbs=bytes Convert bytes at a time, that is, specify the size of the conversion buffer.
skip=blocks Skip blocks from the beginning of the input file before copying.
seek=blocks Skip blocks from the beginning of the output file before copying. Note that it is usually valid only when the output file is disk or tape, that is, when backing up to disk or tape.
count=blocks Only blocks are copied, and the block size is equal to the number of bytes specified by IBS.
conv=conversion Convert the file with the specified parameters: ASCII: convert EBCDIC to ASCII. EBCDIC: convert ASCII to EBCDIC. IBM: convert ASCII to alternate EBCDIC. Block: convert each line to CBS, and fill the insufficient part with spaces. Unblock: make the length of each line CBS, and fill the insufficient part with spaces. Lcase: convert uppercase characters to lowercase characters. Ucase: converts lowercase characters to uppercase characters. Swab: swap each pair of bytes of input. Noerror: do not stop when an error occurs. Notrunc: do not truncate the output file. Sync: fill IBS bytes in each input block, and fill the insufficient part with null (nul) characters.

The above are some examples of Linux system maintenance using DD command-line program, including the backup and recovery of the whole hard disk, the burning of the optical disk, clearing all the data in the disk or file, and copying the data in the memory to the hard disk.

Application examples of DD command

For a new system, if we want to know the read and write speed of the hard disk. Then we can use the following methods to test and analyze the system sequential read and write speed. First, we test the read speed on the hard disk through the command in example 1.

[example 1] test the reading speed on the hard disk. The commands are as follows:

[[email protected] ~]# dd if=/dev/zero bs=1024 count=1000000 of=file_1GB
1000000+0 records in
1000000+0 records out
1024000000 bytes (1.0 GB) copied, 36.2072 s, 28.3 MB/s

Of which:

  • /Dev / null: the nickname is bottomless hole. You can output any data to it. It takes all and won’t hold!
  • /Dev / Zero: is an input device that you can use to initialize files. The device provides 0 endlessly and can use any number you need – much more. It can also be used to write the string 0 to a device or file.
  • /Dev / null: an empty device, also known as a bit bucket. Any output written to it is discarded. If you do not want the message to be displayed or written to a file as standard output, you can redirect the message to the bucket.

We can also test the reading speed of the hard disk by using the file = 1GB just generated in the root directory, or by using the command in example 2.

[example 2] test the reading speed. The command is as follows:

[[email protected] ~]# dd if=file_1GB of=/dev/null bs=1024
1000000+0 records in
1000000+0 records out
1024000000 bytes (1.0 GB) copied, 1.20325 s, 851 MB/s

file_ The 1GB file is very large. We can verify it with the LS – L file * command.

After the operation is completed, delete the file using the RM command in example 3. Finally, we’d better be able to verify again with the LS – L file * command to prevent errors.

[example 3] delete. The command is as follows:

[[email protected] ~]# rm -f file_1GB

Next, you need to clone the important partition on the hard disk to another partition on the hard disk, that is, a boot partition on the system.

First, we need to display all the contents in the / boot directory through the LS command in example 4.

[example 4] display the contents of the directory. The command is as follows:

[[email protected] ~]# ls /boot
config-2.6.32-431.el6.x86_64 lost+found
efi symvers-2.6.32-431.el6.x86_64.gz
grub System.map-2.6.32-431.el6.x86_64
initramfs-2.6.32-431.el6.x86_64.img vmlinuz-2.6.32-431.el6.x86_64
initrd-2.6.32-431.el6.x86_64kdump.img

In order to determine the file name of the specific device where / boot is located, we can list the information of the device where / boot is located and its status through the DF command in example 5.

[example 5] equipment status information. The command is as follows:

[[email protected] ~]# df -h /boot
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 291M 39M 238M 14% /boot

After confirming the device where / boot is located, clone the / dev / sdal partition to the / dev / sdbl partition of another hard disk through the DD command in example 6.

[example 6] cloning operation. The command is as follows:

[[email protected] ~]# dd if=/dev/sdal of=/dev/sdbl bs=4096 conv=noerror

Then create a directory to load the / dev / sdbl partition through the MKDIR command in [Example 7] (we used / boot2 for memory purposes).

[Example 7] create a loading partition. The command is as follows:

[[email protected] ~]# mkdir .boot2

Finally, we can use the mount command in example 8 to load the / dev / sdbl partition.

[example 8] load the / dev / sdbl partition. The command is as follows:

[[email protected] ~]# mount /dev/sdbl /boot2

The above isLiangxu tutorial networkShare the detailed explanation of Linux DD command for all friends.

This article is composed of blog one article multi posting platformOpenWriterelease!