Linux dd command

Source: Internet
Author: User
Dd is a very useful command in Linux/Unix. It is used to copy an object with a specified size block and perform the specified conversion at the same time. 1. Command IntroductionThe main option of DD: specify a number. If the following column ends with a number: B = 512, c = 1, K = 1024, W = 2, XM = Number MIF = file: input file name. The default value is standard input. Of = file output file name. The default value is standard output. IBS = bytes read bytes at a time (that is, the size of a block is bytes ). Obs = bytes write bytes at a time (that is, the size of a block is bytes ). BS = bytes both sets the size of the read/write block to bytes, which can replace IBS and obs. CBS = bytes are converted to bytes at a time, that is, the size of the conversion buffer. Skip = blocks skipping blocks from the beginning of the input file before copying. Seek = blocks skips blocks from the beginning of the output file and then starts copying. (Usually only valid when the output file is a disk or tape ). Count = blocks only copies blocks. The block size is equal to the number of bytes specified by IBS. Conv = Conversion [, conversion...] use the specified parameter to convert the file. Conversion parameters: Convert ebcdic to ASCII. Ebcdic converts ASCII to ebcdic. IBM converts each line to a CBS record by converting ASCII to alternate ebcdic. Block. The missing part is filled with spaces. Unblock: the length of each row is CBS, and spaces are used to fill the remaining parts. Lcase converts uppercase to lowercase. Ucase converts lowercase to uppercase. Swab exchanges each pair of input bytes. Do not stop when noerror occurs. Notrunc does not truncate the output file. Sync fills each input block into IBS bytes, and the missing part is filled with null (NUL) characters. 2. instance analysis 2. 1. Data backup and recovery2.1.1 full disk data backup and recovery backup: Code: dd If =/dev/hdx of =/dev/hdy back up local/dev/hdx to/dev/hdycode: dd If =/dev/hdx of =/path/to/Image ------------------------------------------------------------------ This function is similar to CAT function -------- jhyBack up full/dev/hdx data to the image file code: dd If =/dev/hdx | gzip>/path/to/image.gz to back up full/dev/hdx data in the specified path, the gzip tool is used for compression and saved to the specified path. Recovery:Code: dd If =/path/to/image of =/dev/hdx restore the backup file to the specified disk code: gzip-DC/path/to/image.gz | dd OF =/dev/hdx restores the compressed backup file to the specified Disk 2.1.2. Remote Backup Using NetcatCode: dd If =/dev/hda BS = 16065b | Netcat <targethost-ip> 1234 run this command on the source host to back up/dev/hdacode: netcat-l-P 1234 | dd OF =/dev/hdc bs = 16065b execute this command on the target host to receive data and write it to/dev/hdccode: netcat-l-P 1234 | Bzip2> partition. IMG   Netcat-l-P 1234 | gzip> partition. imgThe preceding two commands are used to compress data by using bzip2gzip and save the backup files to the current directory. 2.1.3. backup MBR backup: code: dd If =/dev/hdx of =/path/to/image COUNT = 1 bs = 512 backup the MBR information of the 512byte size starting from the disk to the specified file for recovery: code: dd If =/path/to/image of =/dev/hdx write the backup MBR information to disk start part 2.1.4. backup floppy disk code: dd If =/dev/fd0 of = disk. img count = 1 bs = 1440k back up the data in the drive to the disk in the current directory. imgfile 2.1.5. copy the memory data to the hard disk code: dd If =/dev/MEM of =/root/mem. bin BS = 1024 copy the data in the memory to the MEM in the root directory. binfile 2.1.6. copy the ISO image code from the CD: dd If =/dev/CDROM of =/root/CD. copy the CD data to the root folder and save it as CD. ISO file   2. Increase the size of the SWAp partition FileCode: dd If =/dev/Zero of =/swapfile BS = 1024 COUNT = 262144 create a large enough file (256 MB here) code: mkswap/swapfile converts this file to the swap file code: Swapon/swapfile enable this swap file code:/swapfile swap defaults 0 0 automatically loads the swap file at each boot, add a line to the/etc/fstab file 2. 3. Destroy Disk DataCode: dd If =/dev/urandom of =/dev/hda1 fill the hard disk with random data and can be used to destroy data in some necessary scenarios. After this operation is performed, the/dev/hda1 cannot be mounted, and the creation and copy operations cannot be performed. 2.4 disk management 2.4.1. Get the most appropriate block sizecode: dd If =/dev/Zero BS = 1024 COUNT = 1000000 of =/root/1gb. File   Dd If =/dev/Zero BS = 2048 COUNT = 500000 of =/root/1gb. File    Dd If =/dev/Zero BS = 4096 COUNT = 250000 of =/root/1gb. File     Dd If =/dev/Zero BS = 8192 COUNT = 125000 of =/root/1gb. FileThe optimal Block Size of the system can be determined by comparing the command execution time displayed in the DD command output. 2.4.2 test hard disk read/write speedCode: dd If =/root/1gb. File BS = 64 k | dd OF =/dev/null   Dd If =/dev/Zero of =/root/1gb. File BS = 1024 COUNT = 1000000The execution time of the preceding two commands can be used to calculate the read/write speed of the test hard disk. 2.4.3. hard Disk repair code: dd If =/dev/SDA of =/dev/SDA when the hard disk is not used for a long time (for example, 1 or 2 years), magnetic fluxpoint will be generated on the disk. When the head reads these areas, it may encounter difficulties and may cause I/O errors. When this affects the first sector of the hard disk, the hard disk may be decommissioned. The preceding command may bring the data back to life. This process is safe and efficient.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.