What is dd If =/dev/Zero of =? How to use dd commands in Linux

Source: Internet
Author: User
Tags uppercase letter

I. Explanation of DD commands

DD: Use a block of the specified size to copy an object and perform the specified conversion at the same time. Note: If the following column ends with a specified number, multiply it by the corresponding number: B = 512; C = 1; k = 1024; W = 2 Parameter notes:1. If = File Name: Enter the file name. The default value is standard input. Specifies the source file. <If = Input File> 2. Of = File Name: name of the output file. The default value is standard output. Specifies the target file. <Of = output file> 3. IBS = Bytes: Read bytes at a time, that is, specify a block size as bytes. Obs = Bytes: outputs bytes at a time, that is, specify a block size as bytes. BS = Bytes: set the size of the read/output block to bytes at the same time. 4. CBS = Bytes: bytes are converted at a time, that is, the size of the conversion buffer zone is specified. 5. Skip = blocks: the blocks are skipped from the beginning of the input file and then copied. 6. Seek = blocks: the blocks are skipped from the beginning of the output file and then copied. Note: It is valid only when the output file is a disk or tape, that is, it is backed up to a disk or tape. 7. Count = blocks: copy only blocks. The block size is equal to the number of bytes specified by IBS. 8. Conv = Conversion: Convert the file with the specified parameter. ASCII: Convert ebcdic to ASCII ebcdic: Convert ASCII to ebcdic IBM: Convert ASCII to alternate ebcdic block: convert each line to CBS in length, and fill unblock with spaces in the remaining part: make the length of each line be CBS, and fill lcase with spaces in the deficiencies: Convert uppercase to lowercase ucase: Convert lowercase to swab: noerror: Do not stop notrunc when an error occurs: Do not trunc: Do not truncate the output file Sync: Fill each input block into IBS bytes. fill in the missing part with null (NUL) characters. Ii. dd application example 1. Back up the local/dev/HDB to/dev/HDD.# Dd If =/dev/HDB of =/dev/HDD 2. Back up the full/dev/HDB data to the image file in 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/dev/HDB full data, compress it with gzip, and save it 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 recovery MBRBackup the MBR information of the first 512 bytes starting from the disk to the specified file: # dd If =/dev/hda of =/root/image COUNT = 1 bs = 512 COUNT = 1 means copying only one block; BS = 512 means the block size is 512 bytes. Recovery: # dd If =/root/image of =/dev/had write the backup MBR information to the start part of the disk. 7. backup floppy disk# Dd If =/dev/fd0 of = disk. img count = 1 bs = 1440 K (Block Size: 1.44 MB) 8. Copy the memory content to the hard disk# Dd If =/dev/MEM of =/root/MEM. Bin BS = 1024 (specify the block size as 1 K) 9. Copy the content of the CD 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 FileStep 1: Create a 1024 M file: # dd If =/dev/Zero of =/swapfile BS = 262144 COUNT = Step 2: Change the file to a swap file: # mkswap/swapfile Step 3: Enable this swap file: # Swapon/swapfile Step 4: edit the/etc/fstab file so that the swap file is automatically loaded at each boot: /swapfile swap default 0 0 11. Destroy Disk Data# Dd If =/dev/urandom of =/dev/hda1 Note: using random data to fill the hard disk can be used to destroy data when necessary. 12. Test the read/write speed of the hard disk.# Dd If =/dev/Zero BS = 1024 COUNT = 1000000 of =/root/1 GB. file # dd If =/root/1 GB. file BS = 64 k | dd OF =/dev/null the command execution time output by the preceding two commands can calculate the Read and Write speeds of the hard disk. 13. determine the optimal disk block size:# Dd If =/dev/Zero BS = 1024 COUNT = 1000000 of =/root/1 GB. file # dd If =/dev/Zero BS = 2048 COUNT = 500000 of =/root/1 GB. file # dd If =/dev/Zero BS = 4096 COUNT = 250000 of =/root/1 GB. file # dd If =/dev/Zero BS = 8192 COUNT = 125000 of =/root/1 GB. file: Compare the command execution time shown in the preceding command output to determine the optimal Block Size of the system. 14. Hard Disk repair:# Dd If =/dev/SDA of =/dev/SDA or dd If =/dev/hda of =/dev/hda when the hard disk is longer (more than one year) after the disk is not used, magnetic flux point is generated on the disk. When the head reads these areas, it may encounter difficulties and may cause I/O errors. When this affects the first sector of the hard disk, the hard disk may be decommissioned. The above command may bring these data back to life. This process is safe and efficient. 15. Remote Backup Using Netcat# Dd If =/dev/hda BS = 16065b | Netcat <targethost-ip> 1234 run this command on the source host to back up/dev/hda # Netcat-l-P 1234 | dd =/dev/hdc bs = 16065b execute this command on the target host to receive data and write it to/dev/HDC # Netcat-l-P 1234 | Bzip2> partition. IMG # Netcat-l-P 1234 | gzip> partition. the preceding two commands of IMG are used to compress data using Bzip2 and gzip, respectively, and save the backup files in the current directory. Change the I-byte value in a large video file to 0x41 (that is, the ASCII value of uppercase letter)Echo a | dd OF = bigfile seek = $ I BS = 1 COUNT = 1 Conv = notrunc Iii. Differences between/dev/null and/dev/zero/Dev/null. The nickname is a bottomless pit. You can output any data to it. It can be used without support! /Dev/zero is an input device. You can use it to initialize files. This device provides 0 in an infinite number and can use any number you need-more devices are provided. It can be used to write string 0 to a device or file. /Dev/null ------ it is an empty device, also known as a bit bucket ). Any output written to it will be discarded. If you do not want to display messages in standard output or write files, you can redirect messages to the bucket. # If =/dev/Zero of =./test.txt BS = 1 k count = 1
# Ls-ltotal 4
-RW-r -- 1 Oracle DBA 1024 Jul 15 test.txt # Find/-name access_log 2>/dev/null 3.1 Use/dev/nullRegard/dev/null as a "black hole", which is equivalent to a write-only file. All content written to it will be lost forever ., however, you cannot read anything from it. However,/dev/null is very useful for both the command line and script L Disable Standard output# Cat $ FILENAME>/dev/null file content is lost without being output to standard output. L Forbidden standard error# Rm $ badname 2>/dev/null [standard error] is thrown to the Pacific Ocean. L Disable Standard output and standard error output# Cat $ filename 2>/dev/null if "$ FILENAME" does not exist, no error message is prompted. If "$ FILENAME" exists, the file content is not printed to the standard output. Therefore, the above Code will not output any information at all. It is useful when you only want to test the exit code of a command and do not want to have any output. # Cat $ filename &>/dev/null. This is also acceptable, as specified by Baris Cicek. Automatically clear the log file contentL 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 no new processes are generated (because: built-in)
# Cat/dev/null>/var/log/wtmp Hide the cookie and stop using it.It is particularly suitable for handling these nasty "cookies" sent by commercial Web sites # If [-f ~ /. Netscape/cookies] # Delete the cookies if they exist.
# Then
# Rm-f ~ /. Netscape/cookies
# Fi
# Ln-S/dev/null ~ /. Netscape/cookies all cookies will be thrown into the black hole and will not be saved on the disk. 3.2 use/dev/zeroLike/dev/null,/dev/zero is also a pseudo file, but it actually produces continuous null streams (Binary zero streams instead of ASCII streams ). Writing its output will be lost, and reading a series of null values from/dev/zero is also difficult, although this can also be done through OD or a hexadecimal editor. /Dev/zero is mainly used to create an empty file with a specified length for initialization, just like a temporary swap file. Use/dev/zero to create a temporary file for swap#! /Bin/bash # create an swap file.
Root_uid = 0 # the root user's $ uid is 0.
E_wrong_user = 65 # Not root?
File =/swap blocksize = 1024 minblocks = 40 success = 0 # This script must be run as root. if ["$ uid"-ne "$ root_uid"] Then ECHO; echo "you must be root to run this script. "; echo
Exit $ e_wrong_user fi blocks =1 {1:-$ minblocks} # If the command line is not specified, # + is set to 40 by default.
# The above sentence is equivalent :#--------------------------------------------------
# If [-n "$1"] # Then # blocks = $1 # else # blocks = $ minblocks # fi # else if ["$ blocks"-lt $ minblocks] Then blocks = $ minblocks # There must be at least 40 blocks in length.
FI echo "creating swap file of size $ blocks (KB )."
Dd If =/dev/Zero of = $ file BS = $ blocksize COUNT = $ blocks #
Writes zero to the file. mkswap $ File $ blocks # creates the file as a swap file (or swap partition ).
Swapon $ file # activate the swap file.
Echo "swap file created and activated. "Exit $ success another application about/dev/zero is to fill a specified size file with zero for a specific purpose, such as mounting a file system to loopback device) or "Safely" to delete an object. Example: Create ramdisk#! /Bin/bash # ramdisk. Sh # "ramdisk" is a part of the system RAM memory,
# + It can be operated as a file system.
# Its advantage is that the access speed is very fast (including reading and writing ).
# Disadvantage: It is volatile. Data will be lost when the computer is restarted or shut down. # + will reduce the available system Ram.
#10 # What is the role of ramdisk? # Save a large dataset on ramdisk, such as a table or dictionary,
# + This will accelerate data query, because searching in the memory is much faster than searching in the disk. e_non_root_user = 70 # You must use root to run the command.
Rootuser_name = root mountpt =/mnt/ramdisk size = 2000 #2 k blocks (which can be modified as appropriate) blocksize = 1024 # each block has a size of 1 K (1024 bytes)
Device =/dev/ram0 # first ram device
Username = 'id-nu 'If ["$ username "! = "$ Rootuser_name"] Then ECHO "must be root to run \" 'basename $ 0' \ "." Exit $ e_non_root_user Fi if [! -D "$ mountpt"] # test whether the mount point already exists, then # + If the script has been run several times, the directory will not be created again. mkdir $ mountpt # + is already created.
FI dd If =/dev/Zero of = $ device COUNT = $ size BS = $ blocksize # Fill the content of the ram device with zero. # Why do we need to do this?
Mke2fs $ device # create an ext2 File System on the RAM device.
Mount $ device $ mountpt # mount the device.
Chmod 777 $ mountpt # allows common users to access this ramdisk.
# However, it can only be uploaded by the root user.
Echo "\" $ mountpt \ "now available for use." # Now ramdisk can be used to access files even for common users.
# Note: ramdisk is easy to lose, so when the computer system is restarted or shut down, the content in ramdisk will disappear .. # copy all the files you want to save to a regular disk directory.
# Run this script to create a ramdisk again after restart.
# Only reload/mnt/ramdisk and no other steps will work correctly.
# If it is improved, this script can be placed in/etc/rc. d/rc. Local, # + to automatically set up a ramdisk when the system starts.
# This is suitable for database servers with high speed requirements.
Exit 0

Http://www.zhiwenweb.cn/Category/Tech/linux-dd.html

Related Article

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.