I. Explanation of the 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
#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 (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
Increase the swap partition file size
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
destroy 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.
Test Drive Read and write speed
#dd If=/dev/zero bs=1024 count=1000000 of=/root/1gb.file
#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.
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
By comparing the execution time of the command shown in the above command output, you can determine the optimal block size for the system.
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.
- 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.img
#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.
Change the value of the i byte of a large video file to 0x41(the uppercase letter a ASCII value)
#echo A | DD of=bigfile seek= $i Bs=1 count=1 conv=notrunc
Create a linux virtual disk and use files to simulate disks
In the experiment of Linux, if there is no spare hard disk to do the test. You can use files under Linux to simulate disks for testing purposes.
The simulation process is illustrated below, and is excerpted from the Oracle database core technologies and practices-a book that teaches you how to become an Oracle 10g OCP.
1) Create a directory where ASM disks are located with the root user.
# Mkdir–p/u01/asmdisks
2) Create 6 400M size files with the DD command, each representing a single disk.
[Email protected] u01]# CD Asmdisks
[[email protected] asmdisks]# dd If=/dev/zero of=asm_disk1 bs=1024k count=400
[[email protected] asmdisks]# dd If=/dev/zero of=asm_disk2 bs=1024k count=400
[[email protected] asmdisks]# dd If=/dev/zero of=asm_disk3 bs=1024k count=400
[[email protected] asmdisks]# dd If=/dev/zero of=asm_disk4 bs=1024k count=400
[[email protected] asmdisks]# dd If=/dev/zero of=asm_disk5 bs=1024k count=400
[[email protected] asmdisks]# dd If=/dev/zero of=asm_disk6 bs=1024k count=400
3) Associate these files with the bare device.
[Email protected] asmdisks]# chmod 777 asm_disk*
[Email protected] asmdisks]# LOSETUP/DEV/LOOP1 Asm_disk1
[Email protected] asmdisks]# LOSETUP/DEV/LOOP2 Asm_disk2
[Email protected] asmdisks]# LOSETUP/DEV/LOOP3 ASM_DISK3
[Email protected] asmdisks]# LOSETUP/DEV/LOOP4 ASM_DISK4
[Email protected] asmdisks]# LOSETUP/DEV/LOOP5 ASM_DISK5
[Email protected] asmdisks]# LOSETUP/DEV/LOOP6 ASM_DISK6
Note: If you want to delete a virtual disk file that is simulated by DD, delete the emulated disk file directly
(That is, Asm_disk1, ASM_DISK2...ASM_DISK6) is not enough, you must also perform losetup-d/dev/loopn, where n is from 1 to 6. Otherwise, the disk space that is consumed by the file cannot be freed
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.
#if =/dev/zero of=./test.txt bs=1k count=1
#ls –l
Total 4
-rw-r--r--1 Oracle DBA 16:56 test.txt
#find/-nameaccess_log 2>/dev/null
3.1 Using/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
#cat $filename >/dev/null
The contents of the file are lost and not output to standard output.
Prohibit standard error
#rm $badname 2>/dev/null
This error message [standard error] was thrown into the Pacific Ocean.
Suppress output of standard output and standard error
#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.
#cat $filename &>/dev/null
This can actually be, by Baris Cicek pointed out
Automatically empty the contents of a log file
Deleting contents of a file, but preserving the file itself, with Allattendant permissions (from Example 2-1 and Example 2 -3):
#cat/dev/null>/var/log/messages
#: >/var/log/messages has the same effect, but does not produce new processes. (because: Built-in)
#cat/dev/null >/var/log/wtmp
Ideal for dealing with these annoying "cookies" sent by commercial web sites
Hidden Cookies and no longer use
#if [-f~/.netscape/cookies] # is deleted if it exists.
#then
#rm-F ~/.netscape/cookies
#fi
#ln-S/dev/null ~/.netscape/cookies
All cookies are now dropped into the black hole and will not be stored on the disk.
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
with /dev/zero Create a swap temporary file
#!/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)."
Ddif=/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 RAMDisk what's the effect?
# 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
Ddif=/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
Instructions for using the DD command under Linux (excerpt)