search
HomeOperation and MaintenanceLinux Operation and MaintenanceDetailed explanation of the dd command in Linux

Detailed explanation of the dd command in Linux

Oct 23, 2020 am 10:46 AM
Detailed explanation of dd command

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.

Detailed explanation of the dd command in Linux

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

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

ibs=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/hdd

2. Back up the full disk data of /dev/hdb 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 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.gz

5. Restores the compressed backup file to the specified disk

#gzip -dc /root/image.gz | dd of=/dev/hdb

6. Backup and restore MBR

512 starting from the backup disk Bytes of MBR information to the specified file:

#dd if=/dev/hda of=/root/image count=1 bs=512

count=1 means copying only one block; bs=512 means the block size is 512 bytes.

Recovery:

#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 (即块大小为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.iso

10. Increase the size of the swap partition file

Step 1: Create a size For a 256M file:

#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 the /etc/fstab file so that the swap file is automatically loaded every time the computer is turned on:

/swapfile    swap    swap    default   0 0

11. Destroy disk data

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

Note: 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/null

The 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.file

By 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/hda

When 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 > 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 Enter /dev/hdc

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

The 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=notrunc

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

2) 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=400

3) 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_disk6

Note: 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!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Linux Operations: Utilizing the Maintenance ModeLinux Operations: Utilizing the Maintenance ModeApr 19, 2025 am 12:08 AM

Linux maintenance mode can be entered through the GRUB menu. The specific steps are: 1) Select the kernel in the GRUB menu and press 'e' to edit, 2) Add 'single' or '1' at the end of the 'linux' line, 3) Press Ctrl X to start. Maintenance mode provides a secure environment for tasks such as system repair, password reset and system upgrade.

Linux: How to Enter Recovery Mode (and Maintenance)Linux: How to Enter Recovery Mode (and Maintenance)Apr 18, 2025 am 12:05 AM

The steps to enter Linux recovery mode are: 1. Restart the system and press the specific key to enter the GRUB menu; 2. Select the option with (recoverymode); 3. Select the operation in the recovery mode menu, such as fsck or root. Recovery mode allows you to start the system in single-user mode, perform file system checks and repairs, edit configuration files, and other operations to help solve system problems.

Linux's Essential Components: Explained for BeginnersLinux's Essential Components: Explained for BeginnersApr 17, 2025 am 12:08 AM

The core components of Linux include the kernel, file system, shell and common tools. 1. The kernel manages hardware resources and provides basic services. 2. The file system organizes and stores data. 3. Shell is the interface for users to interact with the system. 4. Common tools help complete daily tasks.

Linux: A Look at Its Fundamental StructureLinux: A Look at Its Fundamental StructureApr 16, 2025 am 12:01 AM

The basic structure of Linux includes the kernel, file system, and shell. 1) Kernel management hardware resources and use uname-r to view the version. 2) The EXT4 file system supports large files and logs and is created using mkfs.ext4. 3) Shell provides command line interaction such as Bash, and lists files using ls-l.

Linux Operations: System Administration and MaintenanceLinux Operations: System Administration and MaintenanceApr 15, 2025 am 12:10 AM

The key steps in Linux system management and maintenance include: 1) Master the basic knowledge, such as file system structure and user management; 2) Carry out system monitoring and resource management, use top, htop and other tools; 3) Use system logs to troubleshoot, use journalctl and other tools; 4) Write automated scripts and task scheduling, use cron tools; 5) implement security management and protection, configure firewalls through iptables; 6) Carry out performance optimization and best practices, adjust kernel parameters and develop good habits.

Understanding Linux's Maintenance Mode: The EssentialsUnderstanding Linux's Maintenance Mode: The EssentialsApr 14, 2025 am 12:04 AM

Linux maintenance mode is entered by adding init=/bin/bash or single parameters at startup. 1. Enter maintenance mode: Edit the GRUB menu and add startup parameters. 2. Remount the file system to read and write mode: mount-oremount,rw/. 3. Repair the file system: Use the fsck command, such as fsck/dev/sda1. 4. Back up the data and operate with caution to avoid data loss.

How Debian improves Hadoop data processing speedHow Debian improves Hadoop data processing speedApr 13, 2025 am 11:54 AM

This article discusses how to improve Hadoop data processing efficiency on Debian systems. Optimization strategies cover hardware upgrades, operating system parameter adjustments, Hadoop configuration modifications, and the use of efficient algorithms and tools. 1. Hardware resource strengthening ensures that all nodes have consistent hardware configurations, especially paying attention to CPU, memory and network equipment performance. Choosing high-performance hardware components is essential to improve overall processing speed. 2. Operating system tunes file descriptors and network connections: Modify the /etc/security/limits.conf file to increase the upper limit of file descriptors and network connections allowed to be opened at the same time by the system. JVM parameter adjustment: Adjust in hadoop-env.sh file

How to learn Debian syslogHow to learn Debian syslogApr 13, 2025 am 11:51 AM

This guide will guide you to learn how to use Syslog in Debian systems. Syslog is a key service in Linux systems for logging system and application log messages. It helps administrators monitor and analyze system activity to quickly identify and resolve problems. 1. Basic knowledge of Syslog The core functions of Syslog include: centrally collecting and managing log messages; supporting multiple log output formats and target locations (such as files or networks); providing real-time log viewing and filtering functions. 2. Install and configure Syslog (using Rsyslog) The Debian system uses Rsyslog by default. You can install it with the following command: sudoaptupdatesud

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use