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: if a number is specified, the end of the following column is multiplied by the corresponding number: b512; c1; k1024; w2 parameter: 1. if File name: input file name. The default value is standard input. Specifies the source file. &
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 # but can only be root? Load it.
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.