Linux Command Summary DD command detailed

Source: Internet
Author: User
Tags volatile

One: DD command

DD: Copies a file with a block of the specified size and makes the specified conversion at the same time as the copy.

Note: Where the number is specified, the number is multiplied by the following character: b=512;c=1;k=1024;w=2

Parameter comment:
    1. if= file name: Enter a file name, default to standard input. The source file is specified. < If=input file >
    2. of= file name: Output file name, default is standard output. That is, the specified destination file. < Of=output file >
    3. Ibs=bytes: Reads a bytes byte at a time, which specifies a block size of bytes bytes.
      Obs=bytes: Outputs bytes bytes at a time, that is, specifying a block size of bytes bytes.
      Bs=bytes: Set the read/output block size to bytes bytes at the same time.
    4. Cbs=bytes: Converts bytes bytes at a time, that is, specifies the conversion buffer size.
    5. Skip=blocks: Skips blocks blocks from the beginning of the input file before copying begins.
    6. Seek=blocks: Skips blocks blocks from the beginning of the output file before copying begins.
      Note: Usually only works when the output file is a disk or tape, that is, when backing up to disk or tape.
    7. Count=blocks: Copies only blocks blocks, the block size equals the number of bytes specified by IBS.
    8. Conv=conversion: Transforms the file with the specified parameters.
      • ASCII: convert EBCDIC to ASCII
      • EBCDIC: convert ASCII to EBCDIC
      • IBM: Convert ASCII to alternate EBCDIC
      • Block: Converts each row to a CBS length, with less space padding
      • Unblock: Make each line the length of the CBS, the less part filled with spaces
      • LCase: Converting uppercase characters to lowercase characters
      • UCase: Converting lowercase characters to uppercase characters
      • Swab: Swap each byte of the input
      • NoError: Do not stop when error occurs
      • Notrunc: Output File not truncated
      • Sync: Fills each input block into IBS bytes, and the less part is padded with empty (NUL) characters.
Second, DD application example

1. Back up the local/dev/hdb full disk to the/DEV/HDD

DD If=/dev/hdb OF=/DEV/HDD

2. Back up the/DEV/HDB full data 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/DEV/HDB full data and compress it with the Gzip tool to save 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

The backup disk starts with 512 byte-sized MBR information to the specified file:

DD If=/dev/hda of=/root/image count=1 bs=512

Count=1 refers to copying only one block; bs=512 refers to a block size of 512 bytes.

Recovery:

DD If=/root/image Of=/dev/had

Writes the backed up MBR information to the disk start section

7. Backup floppy disk

DD if=/dev/fd0 of=disk.img count=1 bs=1440k (i.e. block size 1.44M)

8. Copy the memory contents to the hard disk

DD If=/dev/mem Of=/root/mem.bin bs=1024 (Specify block size 1k)

9. Copy the contents of the disc to the specified folder and save it as a Cd.iso file

DD If=/dev/cdrom (HDC) Of=/root/cd.iso

10. Increase the size of the swap partition file

The first step: Create a file of size 256M:

DD If=/dev/zero of=/swapfile bs=1024 count=262144

Step Two: Turn this file into a swap file:

Mkswap/swapfile

Step three: Enable this swap file:

Swapon/swapfile

Fourth step: Edit the/etc/fstab file to automatically load swap files at each boot:

/swapfile Swap swap default 0 0

11. Destroying disk data

DD If=/dev/urandom OF=/DEV/HDA1

Note: Populating the hard disk with random data can be used to destroy the data in some necessary situations.

12. Test the read/write speed of the hard drive

DD If=/dev/zero bs=1024 count=1000000 of=/root/1gb.filedd if=/root/1gb.file bs=64k | DD Of=/dev/null

With the command execution time of the above two commands output, 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.filedd if=/dev/zero bs=2048 count=500000 of=/root/1Gb.filedd If=/dev /zero bs=4096 count=250000 of=/root/1gb.filedd if=/dev/zero bs=8192 count=125000 of=/root/1gb.file

By comparing the execution time of the command shown in the above command output, you can determine the optimal block size for the system.

14. Repair the hard drive:

DD IF=/DEV/SDA OF=/DEV/SDA or DD If=/dev/hda Of=/dev/hda

When the hard disk is placed unused for a longer period of time (more than one year), magnetic flux point is generated on the disk, which can cause difficulties when the heads are read to these areas and may result in I/O errors. When this condition affects the first sector of the hard disk, it may cause the hard disk to retire. The above command may bring the data back to the dead. And the process is safe and efficient.

15. Remote Backup with Netcat

DD If=/dev/hda bs=16065b | Netcat < TARGETHOST-IP > 1234

Perform this command on the source host 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.imgnetcat-l-P 1234 | gzip > Partition.img

The above two instructions are the change of the destination host instruction using BZIP2, gzip to compress the data, and save the backup file in the current directory.

16. Change the value of byte I in a large video file to 0x41 (that is, the ASCII value of capital a)

echo A | DD of=bigfile seek= $i Bs=1 count=1 conv=notrunc
Iii. the difference between/dev/null and/dev/zero

/dev/null, nicknamed the Bottomless pit, you can output any data to it, it take all, and will not hold!

/dev/zero, is an input device that you can use to initialize files. The device provides an exhaustive 0 and can use any number you need-the device offers much more. He can be used to write a string 0 to a device or file.

/dev/null--It is an empty device, also known as a bit bucket (bits bucket). Any output written to it will be discarded. If you do not want the message to display or write to the file in standard output, you can redirect the message to the bin.

[Email protected]:if=/dev/zero of=./test.txt bs=1k count=1[email protected]:ls–ltotal 4-rw-r--r--1 Oracle DBA 1 5 16:56 test.txt[email protected]:find/-name access_log 2>/dev/null
Using/dev/nul

Think of/dev/null as a "black hole", which is equivalent to a write-only file, and all content written to it will be lost forever. and trying to read from it is nothing. However,/dev/null is very useful for command lines and scripts.

Prohibit standard output

[Email protected]:cat $filename >/dev/null #文件内容丢失, not output to standard output.

Prohibit standard error

[Email protected]:rm $badname 2>/dev/null #这样错误信息 [standard error] was dumped in the Pacific Ocean.

Suppress output of standard output and standard error

[Email protected]:cat $filename 2>/dev/null >/dev/null

If the "$filename" does not exist, there will be no error message, and if "$filename" exists, the contents of the file will not be printed to standard output. Therefore, the above code does not output any information at all. This is useful when you want to test only the exit code of a command without any output.

Using/dev/zero

Like/dev/null,/dev/zero is also a pseudo-file, but it actually produces a continuous stream of NULL (binary 0 streams, not ASCII). The output written to it is lost, and it is difficult to read a series of NULL from/dev/zero, although this can be done through OD or a hex editor.

The main use of/dev/zero is to create an empty file with a specified length for initialization, just like a temporary swap file.

Create a swap temporary file with/dev/zero

#!/bin/bash# Create a swap file. Root_uid=0 # Root User $UID is 0.e_wrong_user=65 # not root? file=/swapblocksize=1024minblocks=40success=0# This script must be run with ROOT. If ["$UID"-ne "$ROOT _uid"]thenecho; echo "You must is root to run this script."; Echoexit $E _wrong_userfiblocks=${1:-$MINBLOCKS} # If the command line is not specified, #+ is set to the default of 40 blocks. # The above sentence is equivalent to: #------------------------------ --------------------# If [-N ' $ ']# then# blocks=$1# else# blocks= $MINBLOCKS # fi#-------------------------------------  -------------if ["$blocks"-lt $MINBLOCKS]thenblocks= $MINBLOCKS # A minimum of 40 blocks long. Fiecho "Creating swap file of size $blocks Blocks (KB). " DD If=/dev/zero of= $FILE bs= $BLOCKSIZE count= $blocks # writes zero to the file. Mkswap $FILE $blocks # This file is built as a swap file (or swap partition). swapon $FILE # Active Change the file. echo "Swap file created and activated." Exit $SUCCESS

Another application for/dev/zero is to use 0 to populate a file of a specified size for a specific purpose, such as mounting a file system to a loopback device (loopback) or "safely" deleting a file.

Example Create RAMDisk

#!/bin/bash# ramdisk.sh# "RAMDisk" is a part of the system's RAM memory, #+ it can be manipulated as a file system. # It has the advantage of very fast access (including Read and write). # Cons: Volatile, loss of data when the computer restarts or shuts down. #+ Reduces the system's available ram.# 10 # So what does RAMDisk do? # Save a large data set in RAMDisk, such as a table or dictionary, #+ can speed up data queries, because finding in memory is much faster than finding it on disk. E_NON_ROOT_USER=70 # must be run with ROOT. rootuser_name=rootmountpt=/mnt/ramdisksize=2000 # 2K Blocks (can be modified appropriately) blocksize=1024 # there are 1 K (byte) size per block device=/dev/ RAM0 # The first RAM device username= ' Id-nu ' if ["$username"! = "$ROOTUSER _name"]thenecho "must is root to run" ' BaseName $ ' ". Exit $E _NON_ROOT_USERFIIF [!-D "$MOUNTPT"] # test whether the mount point already exists, then #+ if the script has been run several times, it will not build this directory mkdir $MOUNTPT #+ because the front has been established. Fidd If=/dev/zero of= $DEVICE count= $SIZE bs= $BLOCKSIZE # Fills the contents of the RAM device with 0. # Why do you need to do this? MKE2FS $DEVICE # Create a ext2 file system on a RAM device. Mount $DEVICE $MOUNTPT # mount device. chmod 777 $MOUNTPT # allows ordinary users to access this ramdisk.# However, it can only be downloaded by root unloading. echo "" $MOUN TPT "now available for use." # now RAMDisk even ordinary users can access files. Note that RAMDisk is volatile, so when the computer system restarts or shuts down, the contents of RAMDisk disappear. # Copy all of the files you want to save to a regular disk directory. # after rebooting, Run this script again to build up a ramdisk.# reload only/mnt/rAmdisk and no other steps will not work correctly. # If improved, this script can be placed in/etc/rc.d/rc.local,#+ to enable the system to automatically set up a ramdisk.# so that it is suitable for high-speed database server. Exit 0 

Via http://www.linuxde.net/2013/03/12928.html

Linux Command Summary DD command detailed

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.