Dd commands for Linux

Source: Internet
Author: User

Dd command usage

I used the DD command when installing the file system. If I am not very familiar with it, I checked its usage and shared it with everyone.

I. dd command

1. Enter the command on the terminal: Man dd. Refer to the official explanations below.
:
Name
DD-convert and copy a file
Synopsis
Dd [operand]...
Dd Option
Description
Copy a file, converting and formatting according to the operands.
BS = bytes
Read and Write bytes at a time (also see IBS =, obs =)
CBS = bytes
Convert bytes at a time
Conv = convs
Convert the file as per the comma separated symbol list
Count = Blocks
Copy only blocks input Blocks
IBS = bytes
Read bytes at a time (default: 512)
If = File
Read from file instead of stdin
Iflag = flags
Read as per the comma separated symbol list
Obs = bytes
Write bytes at a time (default: 512)
Of = File
Write to file instead of stdout
Oflag = flags
Write as per the comma separated symbol list
Seek = Blocks
Skip blocks obs-sized blocks at start of output
Skip = Blocks
Skip blocks IBS-sized blocks at start of input
Status = noxfer
Suppress transfer statistics
Blocks and bytes may be followed by the following multiplicative suf ‐
Fixes: c = 1, W = 2, B = 512, kb = 1000, K = 1024, MB = 1000*1000, m
= 1024*1024, XM = m GB = 1000*1000*1000, G = 1024*1024*1024, and so on
T, P, E, Z, Y.
Each Conv symbol may be:
ASCII from ebcdic to ASCII
Ebcdic from ASCII to ebcdic
IBM from ASCII to alternate ebcdic
Block pad newline-terminated records with spaces to CBS-size
Unblock
Replace trailing spaces in CBS-size records with newline
Lcase change upper case to lower case
Nocreat
Do not create the output file
Excl fail if the output file already exists
Notrunc
Do not truncate the output file
Ucase change lower case to upper case
Swab swap every pair of input bytes
Noerror
Continue after read errors
Sync pad every input block with nuls to IBS-size; when used
Block or unblock, pad with spaces rather than nuls
Fdatasync
Physically write output file data before finishing
Fsync likewise, but also write metadata


Tiger-John note:

Dd is used to copy an object with a specified block size and perform the specified conversion at the same time.
1. Command Introduction
Main options of DD:
If the following column ends with a specified number, multiply it by the corresponding number:
B = 512, c = 1, K = 1024, W = 2, XM = Number m
If = 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
Set the size of the read/write block to bytes, which can replace IBS and obs.
CBS = bytes
Bytes are converted at a time, that is, the size of the conversion buffer.
Skip = Blocks
The blocks are skipped from the beginning of the input file and then copied.
Seek = Blocks
The blocks are skipped from the beginning of the output file and then copied. (Usually only valid when the output file is a disk or tape ).
Count = Blocks
Copy only 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:
ASCII conversion ebcdic is ASCII.
Ebcdic converts ASCII to ebcdic.
IBM converts ASCII to alternate ebcdic.
Block converts each row to a record with a length of CBS. 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.
Ii. instance analysis
1. Data backup and recovery
1> full disk data backup and recovery
A. Backup
Back up the local/dev/hdx disk to/dev/hdy: dd If =/dev/hdx of =/dev/hdy
Back up full/dev/hdx data to the image file in the specified path: dd If =/dev/hdx of =/path/to/Image
Back up/dev/hdx full data, compress it with gzip, and save it to the specified path: dd If =/dev/hdx | Gzip
>/Path/to/image.gz
B. Restore
Restore the backup file to the specified Disk: dd If =/path/to/image of =/dev/hdx
Restore the compressed backup file to the specified Disk: gzip-DC/path/to/image.gz | dd OF =/dev/hdx
2> Remote Backup Using Netcat
Run this command on the source host to back up/dev/hda: dd If =/dev/hda BS = 16065b | Netcat <targethost-ip>
1234
Run this command on the target host to receive data and write it to/dev/HDC: Netcat-l-P 1234 | dd OF =/dev/HDC
BS = 16065b
The following two commands are used to compress data by using Bzip2 gzip and save the backup files to the current directory:
Netcat-l-P 1234 | Bzip2> partition. img
Netcat-l-P 1234 | gzip> partition. img
3> back up MBR
A. Backup:
The MBR information starting from the backup disk is sent to the specified file: dd If =/dev/hdx of =/path/to/Image
Count = 1 bs = 512
B. Recovery:
Write the backup MBR information to the start part of the Disk: dd If =/path/to/image of =/dev/hdx
4> backup floppy disk
Back up the disk. imgfile from the drive to the current directory: dd If =/dev/fd0 of = disk. img count = 1 bs = 1440 K
5> copy memory data to Hard Disk
Copy the data in the memory to the mem. binfile in the root directory: dd If =/dev/MEM of =/root/MEM. Bin
BS = 1024
6> copy an ISO image from a cd
Copy the disc data to the root folder and save it as the CD. ISO file: dd If =/dev/CDROM of =/root/CD. ISO
2. Increase the size of the SWAp partition File
Create a large enough file (256 MB here): dd If =/dev/Zero of =/swapfile BS = 1024 COUNT = 262144
Change this file to a swap file: mkswap/swapfile
Enable this swap file: Swapon/swapfile
When the swap file is automatically loaded at each boot, you need to add a line in the/etc/fstab file:/swapfile swap
Swap defaults 0 0
3. Destroy Disk Data
Fill hard disk with random data: dd If =/dev/urandom of =/dev/hda1
Data can be destroyed when necessary. After this operation is performed, the/dev/hda1 cannot be mounted, and the creation and copy operations cannot be performed.
4. disk management
1> get the most appropriate block size
The optimal Block Size of the system can be determined by comparing the command execution time displayed in the DD command output:
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. File
2> test hard disk read/write speed
The read/write speed of the test hard disk can be calculated based on the execution time of the two command outputs:
Dd If =/root/1gb. File BS = 64 k | dd OF =/dev/null
HDD if =/dev/Zero of =/root/1gb. File BS = 1024 COUNT = 1000000
3> hard disk repair
When a hard disk is not used for a long time (for example, one or two years), a magnetic flux point is 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 following command may bring the data back to life. This process is safe and efficient.
Dd If =/dev/SDA of =/dev/SDA


 

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.