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

Use case Analysis of dd Command in linux

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article mainly introduces the use case analysis of dd command in linux, which has certain reference value and can be used for reference by friends who need it. I hope you will learn a lot after reading this article. Next, let the editor take you to learn about it.

Linux dd command details: 1, dd with the specified size of the block copy a file, and at the same time the specified conversion; 2, [if= file name] input file name, the default is standard input; 3, [of= file name] output file name, the default is standard output.

Detailed explanation of dd command in linux:

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: bouncy 512

Parameter comments:

1. If= file name: enter the file name. Default is 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 >

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.

3. 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, with the block size 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 ebcdicibm: convert ascii to alternate ebcdicblock: 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.

II. Application examples of dd

1. Back up the local / dev/hdb to / dev/hdd

# dd if=/dev/hdb of=/dev/hdd

2. Back up the whole data of / dev/hdb to the image file of the specified path.

# 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

Back up 512 bytes of MBR information at the beginning of the 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 a block size of 512 bytes.

Restore:

# dd if=/root/image of=/dev/had

Write the backup MBR information to the beginning of the disk

7. Backup floppy disk

# dd if=/dev/fd0 of=disk.img count=1 bs=1440k (i.e. the block size is 1.44m)

8. Copy the contents of memory to the hard disk

# dd if=/dev/mem of=/root/mem.bin bs=1024 (specify a block size of 1k)

9. Copy the contents 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 swap partition file size

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 the swap file:

# swapon / swapfile

Step 4: edit the / etc/fstab file so that the swap file is loaded automatically each time you boot:

/ swapfile swap swap default 0 0

11. Destroy disk data

# dd if=/dev/urandom of=/dev/hda1

Note: use random data to fill the hard disk, which can be used to destroy data in some necessary situations.

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

Through the command execution time outputted by the above two commands, the read and write speed of the hard disk can be calculated.

13. Determine the best 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

By comparing the command execution time shown in the above command output, you can determine the best block size for the system.

14. Repair the 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), magnetic flux point will be generated on the disk, and it will be difficult for the head to read these areas, which may lead to an Ibind 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.

15. Remote backup using netcat

# dd if=/dev/hda bs=16065b | netcat

< targethost-IP >

1234

Execute this command on the source host to back up / dev/hda

# netcat-l-p 1234 | dd of=/dev/hdc bs=16065b

Execute this command on the destination host to receive data and write to / dev/hdc

# netcat-l-p 1234 | bzip2 > partition.img#netcat-l-p 1234 | gzip > partition.img

The above two instructions are the changes of the destination host instructions. Bzip2 and gzip are used to compress the data, and the backup files are saved in the current directory.

16. Change the value of the first byte of a large video file to 0x41 (ASCII value of capital A)

# echo A | dd of=bigfile seek=$i bs=1 count=1 conv=notrunc

17. Create a linux virtual disk and simulate the disk with files

In the linux experiment, if there is no extra hard drive to do the test. You can use files under linux to simulate the disk for testing purposes.

The simulation process is shown below, excerpted from the book "detailed introduction to the Core Technology and practice of Oracle Database-how to become Oracle 10g OCP".

1) create a directory where the ASM disk is located as the root user.

# mkdir-p / u01/asmdisks

2) create six 400m files, each representing a disk, through the dd command.

[root@book u01] # cd asmdisks [root@book asmdisks] # dd if=/dev/zero of=asm_disk1 bs=1024k count=400 [root@book asmdisks] # dd if=/dev/zero of=asm_disk2 bs=1024k count=400 [root@book asmdisks] # dd if=/dev/zero of=asm_disk3 bs=1024k count=400 [root@book asmdisks] # dd if=/dev/zero of=asm_disk4 bs=1024k count=400 [root@book asmdisks] # dd if=/dev/zero of=asm_disk5 bs=1024k count=400 [root@book asmdisks] # dd if=/dev/zero of=asm_disk6 bs=1024k count=400

3) associate these files with bare devices.

[root@book asmdisks] # chmod 777 asm_disk* [root @ book asmdisks] # losetup / dev/loop1 asm_ disk1 [root @ book asmdisks] # losetup / dev/loop2 asm_ disk2 [root @ book asmdisks] # losetup / dev/loop3 asm_ disk3 [root @ book asmdisks] # losetup / dev/loop4 asm_ disk4 [root @ book asmdisks] # losetup / dev/loop5 asm_ disk5 [root @ book asmdisks] # losetup / dev/loop6 asm_disk6

Note: if you want to delete the virtual disk file simulated by dd, delete the simulated disk file directly.

That is, asm_disk1, asm_disk2... Asm_disk6) is not enough, you must also execute losetup-d / dev/loopN, where N is from 1 to 6. Otherwise, the disk space occupied by the disk file cannot be released.

3. The difference between / dev/null and / dev/zero

/ 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-ltotal 4murrwmurf-1 oracle dba 1024 Jul 15 16:56 test.txt#find /-name access_log 2 > / dev/null

3.1 use / dev/null

Think of / 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 the command line and scripts

Prohibit standard output

# cat $filename > / dev/null

The contents of the file are lost and are not output to standard output.

Standard error prohibited

# rm $badname 2 > / dev/null

In this way, the error message [standard error] is thrown into the Pacific Ocean.

Prohibit the output of standard output and standard error

# cat $filename 2 > / dev/null > / dev/null

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

In fact, this is also possible, as pointed out by Baris Cicek

Automatically clear the contents of the log file

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 generate new processes. (because: it's built-in) # cat / dev/null > / var/log/wtmp

Particularly suitable for dealing with these pesky "cookies" sent by commercial Web sites

Hide cookie instead of using

# if [- f ~ / .netscape/cookies] # delete if it exists. # 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 use / dev/zero

Like / dev/null, / dev/zero is 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 specified length for initialization, just like a temporary swap file

Create an exchange temporary file with / dev/zero

Thank you for reading this article carefully. I hope it will be helpful for everyone to share the use case study of the dd command in linux. At the same time, I also hope that you will support us, pay attention to the industry information channel, and find out if you encounter problems. Detailed solutions are waiting for you to learn!

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: 0

*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