How to use the DD command in Linux

Source: Internet
Author: User

DD command: Convert and copy a file


Usage:

Dd?if=/path/from/src?of=/path/to/destbs=#:?block?size,? Copy cell size count=#: How many bsof=file are copied? Write to the named file instead of to the standard output if=file? Read from the named file instead of the standard input bs=size? Specifies the block size (both IBS and OBS) ibs=size? One read size A byteobs=size? One time to write size a bytecbs=size? One conversion size of byteskip= Blocks? Omit blocks from the beginning of the size of the IBS seek=blocks? Omit blocks obs-sized blocks from the beginning count=n? Copy only N Records


Convert the file with the specified parameters:

Conv=conversion[,conversion ...]


Conversion parameters:

ASCII? Conversion? The EBCDIC? Asciiebcdic? conversion? What is ASCII? Ebcdicblock? Converts a row of data to a record with a length of? CBS, and the insufficient portion is filled with blanks. Unblock? The space at the end of each line that replaces the CBS length is the new line LCase? convert uppercase characters to lowercase characters ucase? Convert lowercase characters to uppercase characters nocreat? Do not create output file NoError? Do not stop the output file sync ? fills each input block into IBS bytes, and the less part is padded with empty (NUL) characters.


Backup:

Dd?if=/dev/sdx?of=/dev/sdy

Back up the local/DEV/SDX full disk to the/DEV/SDY

Dd?if=/dev/sdx?of=/path/to/image

To back up the/DEV/SDX full data to the image file of the specified path

Dd?if=/dev/sdx?|? Gzip?>/path/to/image.gz

Back up the/DEV/SDX full data and use the Gzip tool to compress and save to the specified path.


Recovery:

Dd?if=/path/to/image?of=/dev/sdx

Restore the backup file to the specified disk

Gzip?-dc?/path/to/image.gz?|? Dd?of=/dev/sdx

Restores the compressed backup file to the specified disk.


Copy memory data to hard disk:

dd?if=/dev/mem?of=/root/mem.bin?bs=1024

Copy the in-memory data to the Mem.bin file in the root directory.


Copy ISO image from disc:

Dd?if=/dev/cdrom?of=/root/cd.iso

Copy the disc data to the root folder and save it as a Cd.iso file.


Destroying disk data:

Dd?if=/dev/urandom?of=/dev/sda1

The use of random data to populate the hard disk, in some necessary occasions can be used to destroy data. After you do this, the/dev/sda1 cannot be mounted, and the Create and copy operations cannot be performed.


Get the most appropriate block size:

Dd?if=/dev/zero?bs=1024?count=1000000of=/root/1gb.filedd?if=/dev/zero?bs=2048?count=500000of=/root/1gb.filedd? If=/dev/zero?bs=4096?count=250000of=/root/1gb.file


By comparing the command execution time shown in the output of the DD instruction, you can determine the optimal block size for the system:

Test drive Read and write speed:

Dd?if=/dev/zero?of=/root/1gb.file?bs=1024?count=1000000dd?if=/root/1gb.file?bs=64k?|? Dd?of=/dev/null

The Write/read/speed of the test drive can be calculated by the execution time of the last two command outputs.


Repair the hard drive:

Dd?if=/dev/sda?of=/dev/sda

When the hard disk is not used for a long time (for example, 1, 2 years), a degaussing point is generated on the disk. When the heads read these areas, they encounter difficulties and can cause I/O errors. When this condition affects the first sector of the hard disk, it may cause the hard disk to retire. The command above may bring the data back to the dead, and the process is safe and efficient.


Example:

1. Back up the MBR:

[Email protected]?~] #dd? if=/dev/sda1?of=/tmp/mbr.bak?bs=512?count=11+0?records?in1+0?records?out512?bytes? (512?) B)? copied,?0.000160395?s,?3.2? MB/s

2. There is a binary file Filea greater than 2K. Now want to start reading from the 64th byte position, the size to be read is 128Byts. And Fileb, want to read the above 128Bytes write to the beginning of the 32nd byte position, replace 128Bytes, how can I do?

[Email protected]?~] #dd? if=filea?of=fileb?bs=1?count=128?skip=63?seek=31?conv=notrunc128+0?records?in128+0? Records?out128?bytes? (128?) B)? copied,?0.000421017?s,?304?kb/s

3. Create a 2G file system with block size of 2048byte, reserved 1% free space, file system ext4, Volume label test, require this partition to automatically mount to/test directory after boot, and ACL mount option by default


???? [[email protected]?~] #dd? if=/dev/zero?of=test?bs=1m?count=2048???? 2048+0?records?in???? 2048+0?records?out???? 2147483648?bytes? (2.1?) GB)? copied,?121.521?s,?17.7? MB/s???? [[email protected]?~] #mkfs. ext4?-b?2048?-l? ' TEST '?-m?1?test???? Mke2fs?1.41.12? (17-may-2010)???? Test?is?not?a?block?special?device.???? Proceed?anyway?? (y,n)? y???? Filesystem?label=test???? Os?type:? Linux???? block?size=2048? (log=1)???? fragment?size=2048? (log=1)???? Stride=0?blocks, huh? Stripe?width=0?blocks???? 131072?inodes,?1048576?blocks???? 10485?blocks? (1.00%)? Reserved?for?the?super?user???? First?data?block=0???? maximum?filesystem?blocks=537919488???? 64?block?groups???? 16384?blocks?per?group,?16384?fragments?per?group???? 2048?inodes?per?group???? Superblock?backups?stored?on?blocks:????? 16384,?49152,?81920,?114688,?147456,?409600,?442368,?802816???????? Writing?inode?tables:?done???????????????????????????????? Creating?journal? (32768?blocks): Done???? Writing?superblocks?and?filesystem?accounting?information:?done???????? This?filesystem?will?be?automatically?checked?every?35?mounts?or???? 180?days,?whichever?comes?first.?? Use?tune2fs?-c?or?-i?to?override.???? [[email protected]?~] #tune2fs?-o?acl?test????? Tune2fs?1.41.12? (17-may-2010)???? [[email protected]?~] #echo? ' /root/test?/mnt/test?? Ext4?? Defaults?? 0?0 '? >>?/etc/fstab???? [[email protected]?~] #echo? ' /root/test?/mnt/test?? Ext4?? Defaults,loop?? 0?0 '? >>?/etc/fstab???? [[email protected]?~] #mount?-A


How to use the DD command in Linux

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.