dd: Copies a file in blocks of the specified size, and performs the specified conversion while copying.
Note: If the place where the specified number ends with the following characters, it will be multiplied by the corresponding number: b=512; c=1; k=1024; w=2
1. Parameter Notes:
if=filename: input filename, default is standard input. That is, specify the source file. <if=input file>
of=filename: output filename, default is standard output. Namely specify the destination file. < of=output file >
ibs=bytes: Read bytes bytes at a time, that is, specify a block size of bytes bytes.
obs=bytes: Output bytes bytes at a time, that is, specify a block size of bytes bytes.
bs=bytes: At the same time, set the block size of read in/out to bytes bytes.
cbs=bytes: Convert bytes bytes at a time, that is, specify the conversion buffer size.
skip=blocks: Skip blocks blocks from the beginning of the input file before starting the copy.
seek=blocks: Skip blocks blocks from the beginning of the output file and start copying.
Note: It is usually only valid when the output file is disk or tape, that is, when backing up to disk or tape.
count=blocks: Only blocks 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 with a length of cbs, and fill in the insufficient part with spaces
unblock: make each line the length of cbs, and fill the insufficient part with spaces
lcase: Convert uppercase characters to lowercase characters
ucase: convert lowercase characters to uppercase
swab: Swap each pair of bytes of input
noerror: do not stop on error
notrunc: do not truncate output files
sync: fills each input block to ibs bytes, and fills the missing part with null (NUL) characters
Second, dd application example
1. Backup the entire local /dev/hdb disk to /dev/hdd
dd if=/dev/hdb of=/dev/hdd
2. Back up the entire data of /dev/hdb to the image file of the specified path
dd if=/dev/hdb of=/root/image
3. Restore the backup file to the specified disk
dd if=/root/image of=/dev/hdb
4. Back up the entire data of /dev/hdb, compress it with the gzip tool, and save it to the specified path
dd if=/dev/hdb | gzip > /root/image.gz
5. Restore the compressed backup file to the specified disk
gzip -dc /root/image.gz | dd of=/dev/hdb
6. Backup and restore MBR
Back up the MBR information of the size of 512 bytes at the beginning of the disk to the specified file:
dd if=/dev/hda of=/root/image count=1 bs=512
count=1 means to copy only one block; bs=512 means the block size is 512 bytes.
recover:
dd if=/root/image of=/dev/had
Write the backup MBR information to the beginning of the disk
7. Backup Floppy
dd if=/dev/fd0 of=disk.img count=1 bs=1440k (ie block size is 1.44M)
8. Copy the contents of the memory to the hard disk
dd if=/dev/mem of=/root/mem.bin bs=1024 (specified block size is 1k)
9. Copy the contents of the CD to the specified folder and save it as a cd.iso file
dd if=/dev/cdrom(hdc) of=/root/cd.iso
10. Increase the file size of the swap partition
Step 1: Create a file with a size of 256M:
dd if=/dev/zero of=/swapfile bs=1024 count=262144
Step 2: Turn this file into a swap file:
mkswap /swapfile
Step 3: Enable this swap file:
swapon /swapfile
Step 4: Edit the /etc/fstab file to automatically load the swap file every boot:
/swapfile swap swap default 0 0
11. Destroy disk data
dd if=/dev/urandom of=/dev/hda1
Note: Fill the hard disk with random data, which can be used to destroy data in some necessary occasions.
12. Test the read and write speed of the hard disk
dd if=/dev/zero bs=1024 count=1000000 of=/root/1Gb.file
dd if=/root/1Gb.file bs=64k | dd of=/dev/null
Through the command execution time output by the above two commands, the read and write speed of the hard disk can be calculated.
13. Determine the optimal block size for your hard drive:
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
The optimal block size for your system can be determined by comparing the command execution times shown in the command output above.
14. Repair the hard drive:
dd if=/dev/sda of=/dev/sda 或dd if=/dev/hda of=/dev/hda
When the hard disk is not used for a long time (more than one year), magnetic flux points will be generated on the disk, and the magnetic head will encounter difficulties when reading these areas, and may cause I/O errors. When this situation affects the first sector of the hard disk, it may lead to the scrapping of the hard disk. The above command has the potential to bring this data back to life. And this process is safe and efficient.
15. Remote backup using netcat
dd if=/dev/hda bs=16065b | netcat < targethost-IP > 1234
Execute this command on the source host to backup /dev/hda
netcat -l -p 1234 | dd of=/dev/hdc bs=16065b
Execute this command on the destination host to receive data and write to /dev/hdc
netcat -l -p 1234 | bzip2 > partition.img
netcat -l -p 1234 | gzip > partition.img
The above two instructions are the changes of the destination host instructions, respectively, use bzip2 and gzip to compress the data, and save the backup file in the current directory.
16. Change the value of the i-th byte in a large video file to 0x41 (that is, the ASCII value of the capital letter A)
echo A | dd of=bigfile seek=$i bs=1 count=1 conv=notrunc