Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Interpretation of dd command

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

I. interpretation of the dd command

Dd: copies a file with a block of the specified size and performs the specified conversion at the same time. Note: if the specified number ends with the following characters, it will be multiplied by the corresponding number: bounded 512 cross-sectional 1%% 1024%% 2 parameter notes: 1. If= file name: enter the file name, default is the standard input. That is, specify the source file.

< if=input file >

2. Of= file name: output file name, default is standard output. That is, specify the destination file.

< of=output file >

3. Ibs=bytes: read bytes bytes at a time, that is, specify a block size of bytes bytes. Obs=bytes: output bytes bytes at a time, 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: convert bytes bytes at a time, that is, specify the conversion buffer size. 5. Skip=blocks: skip blocks blocks from the beginning of the input file and then start copying. 6. Seek=blocks: skip blocks blocks from the beginning of the output file and then start copying. Note: it is usually only valid when the output file is a disk or tape, that is, when backing up to a disk or tape. 7. Count=blocks: only blocks blocks are copied, and the block size is equal to the number of bytes specified by ibs. 8. Conv=conversion: converts the file with the specified parameters. 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 blanks in the deficiency: make each line cbs in length The insufficient part fills lcase with spaces: convert uppercase characters to lowercase characters ucase: convert lowercase characters to uppercase characters swab: swap input for every pair of bytes noerror: do not stop notrunc: do not truncate the output file sync: fill each input block to ibs bytes, the insufficient part is filled with blank (NUL) characters. Second, dd application example 1. Back up the local / dev/hdb disk to / dev/hdd # dd if=/dev/hdb of=/dev/hdd 2. Back up the full data of / dev/hdb to the image file # dd if=/dev/hdb of=/root/image 3. Restore the backup files to the specified disk # dd if=/root/image of=/dev/hdb 4. Back up / dev/hdb the whole data, compress it with gzip tool, and save it to the specified path # dd if=/dev/hdb | gzip > / root/image.gz 5. Restore the compressed backup files to the specified disk # gzip-dc / root/image.gz | dd of=/dev/hdb 6. Backup and restore MBR information from the beginning of the MBR backup disk 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 the block size of 512 bytes. Restore: # dd if=/root/image of=/dev/had writes the backup MBR information to the beginning of disk 7. Backup floppy disk # dd if=/dev/fd0 of=disk.img count=1 bs=1440k (that is, the block size is 1.44m) 8. Copy memory contents to hard disk # dd if=/dev/mem of=/root/mem.bin bs=1024 (specified block size is 1k) copy CD contents to specified folder And save to cd.iso file # dd if=/dev/cdrom (hdc) of=/root/cd.iso increase the size of the swap partition file step 1: create a file with a size of 256m: # dd if=/dev/zero of=/swapfile bs=1024 count=262144 step 2: turn this file into a swap file: # mkswap / swapfile step 3: enable this swap file: # swapon / swapfile step 4: edit / etc/fstab file Make the swap file load automatically every time you boot: / swapfile swap swap default 00 destroy disk data # dd if=/dev/urandom of=/dev/hda1 Note: fill the hard disk with random data, which can be used to destroy data in some necessary situations. Test the read and write speed of the hard disk # dd if=/dev/zero bs=1024 count=1000000 of=/root/1Gb.file # dd if=/root/1Gb.file bs=64k | dd of=/dev/null can calculate the read and write speed of the hard disk through the command execution time output from the above two commands. Determine the optimal block size for the 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 can determine the optimal block size for the system by comparing the command execution time shown in the above command output. Repair hard disk: # 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 long time (more than one year), it will produce magnetic flux point on the disk, which will encounter difficulties when the head reads these areas, and may cause an magnetic flux point O error. When this situation affects the first sector of the hard disk, it may cause the hard disk to be scrapped. The above command may bring these numbers back to life. And the process is safe and efficient. Remote backup # dd if=/dev/hda bs=16065b with netcat | netcat

< targethost-IP >

1234 execute 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 changes to the destination host instruction to compress the data using bzip2 and gzip, respectively, and save the backup file in the current directory. Change the value of the first byte in a large video file to 0 × 41 (that is, the ASCII value of uppercase A) echo A | the difference between dd of=bigfile seek=$i bs=1 count=1 conv=notrunc III, / dev/null and / dev/zero dd if=/dev/zero of=/dev/null due to test cpu is full

/ dev/null, nicknamed bottomless pit, you can output any data to it, it takes all, and it won't hold up! / dev/zero, is an input device that you can use to initialize files. The device provides an endless supply of zeros, and you can use any number you need-the device provides a lot more. It can be used to write the 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 the message to be displayed as standard output or written to a file, you can redirect the message to the bucket. # if=/dev/zero of=./test.txt bs=1k count=1

# ls-l total 4

-rw-r-r- 1 oracle dba 1024 Jul 15 16:56 test.txt # find /-name access_log 2 > / dev/null 3.1 use / dev/null to treat / dev/null as a "black hole", which is equivalent to a write-only file, and everything written to it will be lost forever, while trying to read from it will read nothing. However, / dev/null is very useful for both command line and script l to prevent standard output # cat $filename > / dev/null file contents from being lost without output to standard output. L disable standard error # rm $badname 2 > / dev/null error message [standard error] is thrown into the Pacific Ocean l prohibit standard output and standard error output # cat $filename 2 > / dev/null > / dev/null if "$filename" does not exist, there will be no error message; 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 only want to test the exit code of a command without any output. # cat $filename & > / dev/null is actually OK. Baris Cicek indicates that automatically emptying the contents of the log file l 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 a new process. (because: it's built-in)

# cat / dev/null > / var/log/wtmp hides the cookie instead of using the "cookies" # if [- f ~ / .netscape/cookies] # which is particularly suitable for handling the annoying messages sent by commercial Web sites.

# then

# rm-f ~ / .netscape/cookies

# fi

# ln-s / dev/null ~ /. Netscape/cookies now all cookies will be thrown into the black hole instead of being saved on 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 zero stream, 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 also be done through od or a hexadecimal editor. The main use of / dev/zero is to create an empty file of a specified length for initialization, just like a temporary swap file. Create a temporary swap file #! / bin/bash # with / dev/zero to create an swap file. The $UID for a ROOT_UID=0 # Root user is 0. E_WRONG_USER=65 # is 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 be root to run this script."; echo exit $E_WRONG_USER fi blocks=$ {1:-$MINBLOCKS} # if not specified on the command line, # + is set to the default of 40 blocks. The above sentence is equivalent to: #-# if [- n "$1"] # then # blocks=$1 # else # blocks=$MINBLOCKS # fi #-if ["$blocks"-lt $MINBLOCKS] then blocks=$MINBLOCKS # must be at least 40 blocks long. Fi echo "Creating swap file of size $blocks blocks (KB)." Dd if=/dev/zero of=$FILE bs=$BLOCKSIZE count=$blocks # writes zeros to the file. Mkswap $FILE $blocks # creates this file as a swap file (or swap partition). Swapon $FILE # activates the exchange file. Echo "Swap file created and activated." Another application of exit $SUCCESS on / dev/zero is to populate a file of a specified size with zeros for a specific purpose, such as mounting a file system to a loopback device (loopback device) or "safely" deleting a file example to create ramdisk #! / bin/bash # ramdisk.sh # "ramdisk" is a section of 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). # disadvantages: volatile, data will be lost when the computer is restarted or shut down. # + will reduce the RAM available to the system. # 10 # so what is the purpose of ramdisk? # keep a large dataset in ramdisk, such as a table or dictionary. # + this can speed up the data query, because it is much faster to look up in memory than 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 has a size of 1K (1024 byte) 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"] # tests whether the mount point already exists. Then # + if the script has been run several times, the directory will not be built again. Mkdir $MOUNTPT # + because it has been built before. Fi dd if=/dev/zero of=$DEVICE count=$SIZE bs=$BLOCKSIZE # populates the contents of the RAM device with zeros. # Why do you need to do this? Mke2fs $DEVICE # creates an ext2 file system on the RAM device. Mount $DEVICE $MOUNTPT # Mount the device. Chmod 777$ MOUNTPT # enables ordinary users to access this ramdisk. However, it can only be loaded by root. Echo "$MOUNTPT" now available for use. "# now ramdisk can be used to access files even by ordinary users. # Note that ramdisk is volatile, so the content in ramdisk disappears when the computer system is rebooted or shut down. # copy all the files you want to save to a regular disk directory. After restarting, run this script to create a ramdisk again. # only reloading / mnt/ramdisk without other steps will not work correctly. # if improved, the script can be placed in / etc/rc.d/rc.local, # + so that a ramdisk can be automatically set up when the system starts. # this is very suitable for database servers with high speed requirements. Exit 0

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 237

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report