Home > Article > Operation and Maintenance > Detailed explanation of the dd command in Linux
Detailed explanation of the dd command in Linux: 1. dd copies a file using blocks of the specified size, and performs specified conversions while copying; 2. [if=filename] enter the file name, the default is standard Input; 3. [of=file name] output file name, the default is standard output.
##Related learning recommendations: linux video tutorial
Detailed explanation of the dd command in Linux:
1. Explanation of the dd command
dd: Copy a block with a specified size file, and perform specified conversions while copying. Note: If the place where the number is specified ends with the following characters, the corresponding number will be multiplied: b=512; c=1; k=1024; w=2 Parameter comments: 1. if=filename: input filename, the default is standard input. That is, specify the source file. 5e36d5e2433215fe5b03c4e439106bea2, of=file name: output file name, the default is standard output. That is, specify the destination file. bb2d626e57fcb2b0f6b57b38047d4e33ibs=bytes:一次读入bytes个字节,即指定一个块大小为bytes个字节。 obs=bytes:一次输出bytes个字节,即指定一个块大小为bytes个字节。3. bs=bytes: At the same time, set the read/output block size to bytes bytes. 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 before starting copying. 6. seek=blocks: skip blocks blocks from the beginning of the output file before starting copying. Note: It is usually only valid when the output file is a disk or tape, that is, when it is backed up to a disk or tape. 7. count=blocks: Only copies blocks. The block size is equal to the number of bytes specified by ibs. 8. conv=conversion: Convert the file using the specified parameters.
ascii:转换ebcdic为ascii ebcdic:转换ascii为ebcdic ibm:转换ascii为alternate ebcdic block:把每一行转换为长度为cbs,不足部分用空格填充 unblock:使每一行的长度都为cbs,不足部分用空格填充 lcase:把大写字符转换为小写字符 ucase:把小写字符转换为大写字符 swab:交换输入的每对字节 noerror:出错时不停止 notrunc:不截短输出文件 sync:将每个输入块填充到ibs个字节,不足部分用空(NUL)字符补齐。
2. dd application example
1. Back up the entire local /dev/hdb disk to /dev/hdd#dd if=/dev/hdb of=/dev/hdd2. Back up the full disk data of /dev/hdb to the image file in the specified path
#dd if=/dev/hdb of=/root/image3. Restore the backup file to the specified disk
#dd if=/root/image of=/dev/hdb4. Back up the full disk data of /dev/hdb and use gzip The tool compresses and saves it to the specified path
#dd if=/dev/hdb | gzip > /root/image.gz5. Restores the compressed backup file to the specified disk
#gzip -dc /root/image.gz | dd of=/dev/hdb6. Backup and restore MBR512 starting from the backup disk Bytes of MBR information to the specified file:
#dd if=/dev/hda of=/root/image count=1 bs=512count=1 means copying only one block; bs=512 means the block size is 512 bytes. Recovery:
#dd if=/root/image of=/dev/hadWrite the backup MBR information to the beginning of the disk7. Backup floppy disk
#dd if=/dev/fd0 of=disk.img count=1 bs=1440k (即块大小为1.44M)8. Copy the memory contents to the hard disk
#dd if=/dev/mem of=/root/mem.bin bs=1024 (指定块大小为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.iso10. Increase the size of the swap partition fileStep 1: Create a size For a 256M file:
#dd if=/dev/zero of=/swapfile bs=1024 count=262144Step 2: Turn this file into a swap file:
#mkswap /swapfileStep 3: Enable this swap file:
#swapon /swapfileStep 4 : Edit the /etc/fstab file so that the swap file is automatically loaded every time the computer is turned on:
/swapfile swap swap default 0 011. Destroy disk data
#dd if=/dev/urandom of=/dev/hda1Note: Filling the hard disk with random data may, in some cases Data can be destroyed when necessary. 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/nullThe read and write speed of the hard disk can be calculated through the command execution time output by the above two commands. 13. Determine the optimal block size of 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.fileBy comparing the command execution time displayed in the above command output, you can determine the optimal block size of the system. 14. Repair the hard disk:
#dd if=/dev/sda of=/dev/sda 或dd if=/dev/hda of=/dev/hdaWhen the hard disk is left unused for a long time (more than a year), magnetic flux points will be generated on the disk. When the magnetic head reads these areas, it will encounter difficulties and may cause I/O errors. When this condition affects the first sector of the hard drive, it can lead to the failure of the hard drive. The above command may bring this data back to life. And the process is safe and efficient. 15. Use netcat remote backup
#dd if=/dev/hda bs=16065b | netcat < targethost-IP > 1234Execute this command on the source host to back up /dev/hda
#netcat -l -p 1234 | dd of=/dev/hdc bs=16065bExecute this command on the destination host to receive data and write Enter /dev/hdc
#netcat -l -p 1234 | bzip2 > partition.img #netcat -l -p 1234 | gzip > partition.imgThe above two instructions are changes in the destination host instructions, respectively using bzip2 and gzip to compress the data and save the backup file in the current directory. 16. Change the value of the i-th byte of a large video file to 0x41 (the ASCII value of capital letter A)
#echo A | dd of=bigfile seek=$i bs=1 count=1 conv=notrunc17. Create a Linux virtual disk and use the file to simulate the disk In the Linux experiment, if there is no extra hard disk for testing. You can use files under linux to simulate the disk for testing purposes. The simulation process is as follows, excerpted from the book "Detailed Explanation of Oracle Database Core Technology and Practice - Teach You How to Become an Oracle 10g OCP". 1) Create a directory where the ASM disk is located as the root user.
# mkdir –p /u01/asmdisks2) Use the dd command to create six 400M files, each file representing a disk.
[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=4003) Associate these files with raw 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_disk6Note: If you want to delete the virtual disk file simulated by dd, it is not enough to directly delete the simulated disk file (that is, asm_disk1, asm_disk2...asm_disk6), you must also execute losetup -d /dev/loopN, where N ranges from 1 to 6. Otherwise, the disk space occupied by the disk file cannot be released
三、/dev/null和/dev/zero的区别
/dev/null,外号叫无底洞,你可以向它输出任何数据,它通吃,并且不会撑着!
/dev/zero,是一个输入设备,你可你用它来初始化文件。该设备无穷尽地提供0,可以使用任何你需要的数目——设备提供的要多的多。他可以用于向设备或文件写入字符串0。
/dev/null------它是空设备,也称为位桶(bit 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 使用/dev/null
把/dev/null看作"黑洞", 它等价于一个只写文件,所有写入它的内容都会永远丢失.,而尝试从它那儿读取内容则什么也读不到。然而, /dev/null对命令行和脚本都非常的有用
禁止标准输出
#cat $filename >/dev/null
文件内容丢失,而不会输出到标准输出.
禁止标准错误
#rm $badname 2>/dev/null
这样错误信息[标准错误]就被丢到太平洋去了
禁止标准输出和标准错误的输出
#cat $filename 2>/dev/null >/dev/null
因此,上面的代码根本不会输出任何信息。当只想测试命令的退出码而不想有任何输出时非常有用。
#cat $filename &>/dev/null
这样其实也可以, 由 Baris Cicek 指出
自动清空日志文件的内容
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 有同样的效果, 但不会产生新的进程.(因为:是内建的) #cat /dev/null > /var/log/wtmp
特别适合处理这些由商业Web站点发送的讨厌的"cookies"
隐藏cookie而不再使用
#if [ -f ~/.netscape/cookies ] # 如果存在则删除. #then #rm -f ~/.netscape/cookies #fi #ln -s /dev/null ~/.netscape/cookies
现在所有的cookies都会丢入黑洞而不会保存在磁盘上了.
3.2 使用/dev/zero
像/dev/null一样, /dev/zero也是一个伪文件, 但它实际上产生连续不断的null的流(二进制的零流,而不是ASCII型的)。 写入它的输出会丢失不见, 而从/dev/zero读出一连串的null也比较困难, 虽然这也能通过od或一个十六进制编辑器来做到。 /dev/zero主要的用处是用来创建一个指定长度用于初始化的空文件,就像临时交换文件
用/dev/zero创建一个交换临时文件
The above is the detailed content of Detailed explanation of the dd command in Linux. For more information, please follow other related articles on the PHP Chinese website!