Linux DD command Detailed and use case scenario

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, that is, 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: Copy only blocks blocks, the block size equals the number of bytes specified by IBS.

8. 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: 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

[Email protected]: DD if=/dev/hdb OF=/DEV/HDD

2. Back up the/DEV/HDB full data to the image file of the specified path

[Email protected]: DD if=/dev/hdb of=/root/image

3. Restore the backup file to the specified disk

[Email protected]: 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

[Email protected]: DD IF=/DEV/HDB | gzip >/root/image.gz

5. Restore the compressed backup file to the specified disk

[Email protected]: 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:

[Email protected]: 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:

[Email protected]: DD if=/root/image Of=/dev/had

Writes the backed up MBR information to the disk start section

7. Backup floppy disk

[Email protected]: 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

[Email protected]: 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

[Email protected]: 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:

[Email protected]: DD If=/dev/zero of=/swapfile bs=1024 count=262144

Step Two: Turn this file into a swap file:

[Email protected]: mkswap/swapfile

Step three: Enable this swap file:

[Email protected]: 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

[Email protected]: 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

[Email protected]: DD If=/dev/zero bs=1024 count=1000000 of=/root/1gb.file

[Email protected]: DD 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:

[Email protected]: DD If=/dev/zero bs=1024 count=1000000 of=/root/1gb.file

[Email protected]: DD If=/dev/zero bs=2048 count=500000 of=/root/1gb.file

[Email protected]: DD If=/dev/zero bs=4096 count=250000 of=/root/1gb.file

[Email protected]: DD 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:

[Email protected]: 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

[Email protected]: DD If=/dev/hda bs=16065b | Netcat < TARGETHOST-IP > 1234

Perform this command on the source host backup/dev/hda

[Email protected]: 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

[Email protected]: netcat-l-P 1234 | bzip2 > Partition.img

[Email protected]: netcat-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 Three,/dev/null and/dev/zero differences

/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–l

Total 4
-rw-r--r--1 Oracle DBA 16:56 test.txt
[Email protected]:find/-name access_log 2>/dev/null 3.1 use/dev/null

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. 3.2 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.

The $UID of the Root_uid=0 # Root user is 0.

E_WRONG_USER=65 # not Root?

File=/swap

blocksize=1024

Minblocks=40

Success=0

# This script must be run with root.

If ["$UID"-ne "$ROOT _uid"]

Then

Echo echo "You must is root to run this script."; Echo

Exit $E _wrong_user

Fi

blocks=${1:-$MINBLOCKS} # If the command line is not specified,

The #+ 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]

Then

blocks= $MINBLOCKS # A minimum of 40 blocks long.

Fi

echo "Creating swap file of size $blocks blocks (KB)."

DD If=/dev/zero of= $FILE bs= $BLOCKSIZE count= $blocks # writes zeros to the file.

Mkswap $FILE $blocks # to build this file as a swap file (or swap partition).

Swapon $FILE # Activates the swap 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 section of system 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.

#+ will reduce the RAM available to the system.

# 10 # So what does RAMDisk do?

# Save a larger set of data in RAMDisk, such as a table or dictionary,

#+ This speeds up data queries, because looking in memory is much faster than finding it on disk.

E_NON_ROOT_USER=70 # must be run with ROOT.

Rootuser_name=root

Mountpt=/mnt/ramdisk

size=2000 # 2K Blocks (can be modified as appropriate)

blocksize=1024 # 1 K (in the size of a byte) per block

DEVICE=/DEV/RAM0 # First RAM device

Username= ' Id-nu '

If ["$username"! = "$ROOTUSER _name"]

Then

echo "must is root to run" ' BaseName $ ' "."

Exit $E _non_root_user

Fi

if [!-D "$MOUNTPT"] # test whether the mount point already exists,

Then #+ if this script has been running several times, it won't be built again.

mkdir $MOUNTPT #+ Because the front was already established.

Fi

DD If=/dev/zero of= $DEVICE count= $SIZE bs= $BLOCKSIZE

# Fill 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 # enables ordinary users to access this ramdisk.

# However, it can only be downloaded by root unloading.

echo "" $MOUNTPT "now available for use."

# now RAMDisk can be used to access files even for ordinary users.

Note that RAMDisk is volatile, so when the computer system restarts or shuts down, the contents of the RAMDisk disappear.

# Copy all of the files you want to save to a regular disk directory.

# after rebooting, run this script to build up a ramdisk again.

# reload/mnt/ramdisk only and no other steps will work correctly.

# If improved, this script can be placed in the/etc/rc.d/rc.local,

#+ to enable the system to automatically set up a ramdisk when it starts.

# This is a good fit for a database server with high speed requirements.

Exit 0

Linux DD command Detailed and use case scenario

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.