Detailed explanation of DD commands

Source: Internet
Author: User

1. 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: place the specified number. If the following column ends with a character, multiply it by the corresponding number: B = 512; C = 1; k = 1024; W = 2.

Parameters:

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 the size of a block to 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: converts each row to the length of CBS. spaces are used to fill the remaining parts.

Unblock: the length of each row is CBS, and the remaining part is filled with spaces.

Lcase: converts uppercase to lowercase.

Ucase: converts lowercase to uppercase.

Swab: swap each pair of input bytes

Noerror: Do not stop when an error occurs

Notrunc: the output file is not truncated.

Sync: Fill each input block into IBS bytes, and fill the remaining part with null (NUL) characters.

Ii. dd application instance.

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 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 indicates that only one block is copied; BS = 512 indicates that the block size is 512 bytes.

Recovery: dd If =/root/image of =/dev/hda

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 File

Step 1: Create a M file:

Dd If =/dev/Zero of =/swapfile BS = 1024 COUNT = 262144

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: Filling hard disks with random data 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/1gb. File

Dd If =/root/1gb. 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/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

Compare the command execution time shown in the preceding command output to determine the optimal system block size.

14. Hard Disk repair:

Dd If =/dev/SDA of =/dev/SDA or dd If =/dev/hda of =/dev/hda

When the hard disk is not used for a long time (more than one year), magnetic flux point will be generated on the disk. When the head reads these areas, it will 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 preceding command may bring the data back to life. This process is safe and efficient.

 

 

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

 

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

 

/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.
/Dev/Zero ------ the device provides 0 infinitely, and you can use any number you need-the device provides more. It can be used to write string 0 to a device or file.

Oracle @ localhost Oracle] $ if =/dev/Zero of =./test.txt BS = 1 k count = 1
Oracle @ localhost Oracle] $ LS-l
Total 4
-RW-r -- 1 Oracle DBA 1024 Jul 15 test.txt

 

Eg,

Find/-name access_log 2>/dev/null

 

 

 

Use/dev/null
Regard/dev/null as a "black hole ". it is very equivalent to writing only files. 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.

Disable standard output. 1 cat $ FILENAME>/dev/null
2 # file content is lost without being output to standard output.
 

Standard forbidden error (from example 12-3). 1 RM $ badname 2>/dev/null
2 # the error message [standard error] is thrown to the Pacific Ocean.
 

Disable output of standard output and standard error. 1 cat $ filename 2>/dev/null
2 # If "$ FILENAME" does not exist, no error message is prompted.
3 # If "$ FILENAME" exists, the file content will not be printed to the standard output.
4 # therefore, the above Code will not output any information.
5 #
6 # It is useful when you only want to test the exit code of the command and do not want to have any output.
7 #
8 #
9 # Cat $ filename &>/dev/null
10 # Yes, as specified by Baris Cicek.
 

Deleting contents of a file, but preserving the file itself, with all attendant permissions (from Example 2-1 and Example 2-3 ): 1 CAT/dev/null>/var/log/messages
2 #:>/var/log/messages has the same effect, but no new process will be generated (because: built-in)
3
4 CAT/dev/null>/var/log/wtmp
 

Automatically clear the content of log files (especially suitable for processing these annoying "cookies" sent by commercial web sites "):

--------------------------------------------------------------------------------

Example 28-1. Hide the cookie and stop using it

1 If [-f ~ /. Netscape/cookies] # Delete the cookies if they exist.
2 then
3 Rm-f ~ /. Netscape/cookies
4 fi
5
6 ln-S/dev/null ~ /. Netscape/cookies
7 # Now all cookies will be thrown into the black hole instead of stored on the disk.
 

--------------------------------------------------------------------------------

Use/dev/zero
Like/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.

--------------------------------------------------------------------------------

Example 28-2. Use/dev/zero to create a temporary file for swap

1 #! /Bin/bash
2 # create a swap file.
3
4 root_uid = 0 # the root user's $ uid is 0.
5 e_wrong_user = 65 # Not root?
6
7 file =/swap
8 blocksize = 1024
9 minblocks = 40
10 success = 0
11
12
13 # The script must be run as root.
14 if ["$ uid"-ne "$ root_uid"]
15 then
16 echo; echo "you must be root to run this script."; echo
17 exit $ e_wrong_user
18 fi
19
20
21 blocks =$ {1:-$ minblocks} # If the command line is not specified,
22 # + is set to the default 40.
23 # The above sentence is equivalent:
24 #--------------------------------------------------
25 # If [-n "$1"]
26 # Then
27 # blocks = $1
28 # else
29 # blocks = $ minblocks
30 # fi
31 #--------------------------------------------------
32
33
34 if ["$ blocks"-lt $ minblocks]
35 then
36 blocks = $ minblocks # a minimum length of 40 blocks is required.
37 fi
38
39
40 echo "creating swap file of size $ blocks (KB )."
41 dd If =/dev/Zero of = $ file BS = $ blocksize COUNT = $ blocks # writes zero to a file.
42
43 mkswap $ File $ blocks # create this file as a swap file (or a swap partition ).
44 Swapon $ file # activate the swap file.
45
46 echo "swap file created and activated ."
47
48 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) (See example 13-8) or "Safely" to delete an object (see example 12-55 ).

--------------------------------------------------------------------------------

Example 28-3. Create ramdisk

1 #! /Bin/bash
2 # ramdisk. Sh
3
4 # "ramdisk" is a part of the system RAM memory,
5 # + It can be operated as a file system.
6 # its advantage is that the access speed is very fast (including reading and writing ).
7 # disadvantages: Volatile. Data is lost when the computer is restarted or shut down.
8 # + will reduce the available system Ram.
9 #
10 # What is the role of ramdisk?
11 # Save a large dataset on ramdisk, such as a table or dictionary,
12 # + this will accelerate data query, because searching in memory is much faster than searching in disk.
13
14
15 e_non_root_user = 70 # It must be run with root.
16 rootuser_name = root
17
18 mountpt =/mnt/ramdisk
19 size = 2000 #2 k blocks (which can be modified as appropriate)
20 blocksize = 1024 # each block has a size of 1 K (1024 bytes)
21 device =/dev/ram0 # first ram device
22
23 username = 'id-nu'
24 if ["$ username "! = "$ Rootuser_name"]
25 then
26 echo "must be root to run/" 'basename $0 '/"."
27 exit $ e_non_root_user
28 fi
29
30 if [! -D "$ mountpt"] # test whether the mount point already exists,
31 then # + If the script has been run several times, the directory will not be created again.
32 mkdir $ mountpt # + because it has been created earlier.
33 fi
34
35 dd If =/dev/Zero of = $ device COUNT = $ size BS = $ blocksize # Fill the content of the ram device with zero.
36 # Why?
37 mke2fs $ device # create an ext2 File System on the RAM device.
38 Mount $ device $ mountpt # mount the device.
39 chmod 777 $ mountpt # enable normal users to access this ramdisk.
40 # However, it can only be uploaded by the root user.
41
42 echo "/" $ mountpt/"now available for use ."
43 # Now ramdisk can be used to access files even for common users.
44
45 # note that ramdisk is easy to lose, so the content in ramdisk disappears when the computer system is restarted or shut down.
46 #
47 # copy all the files you want to save to a regular disk directory.
48
49 # Run the script to create a ramdisk again after restart.
50 # Only reload/mnt/ramdisk and no other steps will work correctly.
51
52 # If it is improved, this script can be placed in/etc/rc. d/rc. Local,
53 # + to automatically set up a ramdisk when the system starts.
54 # This is suitable for database servers with high speed requirements.
55
56 exit 0

In this way, some error messages will not be displayed.

 

 

 

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.