Instructions for using DD commands under Linux

Source: Internet
Author: User
Tags volatile

What is the meaning of DD If=/dev/zero of=? Instructions for using DD commands under Linux

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, 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: 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. &NBSP;&NBSP;&NBSP;&NBSP;ASCII: Convert EBCDIC to Ascii     ebcdic: convert ASCII to ebcdic   &NBSP;&NBSP;IBM: Convert ASCII to alternate Ebcdic    block: Converts each line to a length of CBS, with less space padding      unblock: Make each line the length of the CBS, the less part filled with spaces     lcase: convert uppercase characters to lowercase      UCase: Converts lowercase characters to uppercase characters     swab: Swap each byte of input      noerror: Do not stop on error       notrunc: Not truncated output file     sync: each input blockFills to IBS bytes, and the insufficient portions are padded with empty (NUL) characters. Second, DD application example 1. Place the local /dev/hdb full disk Backup to /DEV/HDD#dd If=/dev/hdb OF=/DEV/HDD 2. will be /dev/hdb Full -Data backup to the specified path Image file#dd If=/dev/hdb Of=/root/image 3. Restore the backup file to the specified disk#dd If=/root/image Of=/dev/hdb 4. Backup /dev/hdb overall data, and use gzip The tool is compressed and saved to the specified path#dd If=/dev/hdb | gzip >/root/image.gz 5. restores a compressed backup file to a specified disk#gzip-dc/root/image.gz | DD Of=/dev/hdb 6. Backup and Recovery MBRBackup 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 memory contents to hard disk#dd if=/dev/mem Of=/root/mem.bin bs=1024 (Specify block size 1k) 9. Copy disc contents to the specified folder and save as Cd.iso file#dd If=/dev/cdrom (HDC) Of=/root/cd.iso . Increase Swap Partition File SizeStep one: 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 default 0 0 One by one . Destroying disk data#dd if=/dev/urandom of=/dev/hda1 Note: The use of random data to populate the hard disk, in some necessary occasions can be used to destroy data. . 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 The output of the above two command execution time, you can calculate the read and write speed of the hard disk. . determine the optimal block size for your hard disk:#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 determines the optimal block size for the system by comparing the execution time of the command shown in the output of the above command. . Repair the hard drive:#dd IF=/DEV/SDA OF=/DEV/SDA or DD If=/dev/hda Of=/dev/hda when the hard drive is not in use for a longer period of time (more than one year), a magnetic flux point will be generated on the disk, and when the heads are read into these areas, they will encounter difficulties. and may cause 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. A . Use Netcat Remote Backup#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 executes this command on the destination host to receive data and write/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 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. #if =/dev/zero of=./test.txt bs=1k count=1
#ls –ltotal 4
-rw-r--r--1 Oracle DBA 16:56 test.txt#find/-name access_log 2>/dev/null 3.1 using/dev/nullThink 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 file content is lost without output to standard output. l 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 "$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 fileL Deleting contents of a file, but preserving the file itself, with all attendant 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 Hide cookies and no longer use themEspecially suitable for handling these annoying "cookies" sent by commercial Web sites #if [-F ~/.netscape/cookies] # if present, delete.
#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/zeroLike/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.  root_uid=0 # root  User's $UID is 0. e_wrong_user=65 #  not ROOT?  FILE=/swap BLOCKSIZE=1024 MINBLOCKS=40 SUCCESS=0 #  This script must 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, #+  is set to the default of 40 block . #  above this sentence equivalent to:  #-------------------------------------------------- # If [-N "]  # then # blocks=$1 # else # blocks= $MINBLOCKS  # fi #-------------------------------- ------------------ if ["$blocks"-lt $MINBLOCKS] then blocks= $MINBLOCKS #  At least 40 blocks long.  fi& Nbsp;echo "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 # Activates swap files.  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 piece of system RAM memory, #+  It can be treated as a file system to operate . #  It has the advantage of very fast access (including Read and write) . #  disadvantage: volatile, loss of data when the computer restarts or shuts down . #+  will reduce the ram. # of the system available #  So what does RAMDisk do?? #  saving a large dataset in RAMDisk, such as a table or dictionary, #+ , can speed up the data query, because finding 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 appropriately)  blocksize=1024 #  each block has a size of 1 K (in bytes)  device=/dev/ram0 #  First RAM device  username= ' ID -nu '  if ["$username"! = "$ROOTUSER _name"] then    echo "must be 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 will not build this directory anymore     mkdir $MOUNTPT #+  because the front has been built.  fi  dd If=/dev/zero of= $DEVICE count= $SIZE bs= $BLOCKSIZE    #  fills the contents of the RAM device with 0.                                        Why does              #  need to do this? &NBSP;MKE2FS $ Device #  creates 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 "\" $MOUNTPT \ "now available for use."  #  now RAMDisk even ordinary users can access files . #  Note that RAMDisk is volatile, so ramdisk content disappears when the computer system restarts or shuts down . #  Copy all you want to save the file to a regular disk directory under . #  reboot, run this script again to build up a ramdisk. #  only reload/mnt/ramdisk and no other steps will not work correctly.  #  If improved, this script can be placed in/etc/rc.d/rc.local, #+  so that the system can automatically set up a ramdisk. #  This is appropriate for a database server with high speed requirements.  exit 0

Using the DD command under 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.