SoFunction
Updated on 2025-05-14

In-depth analysis and application practice of Linux dd command

dd command overview

Command origin and positioning

The dd command first appeared in the UNIX operating system and was later ported to the Linux platform. It is different from ordinary file copy commands (such as cp), dd operates data in the form of an underlying block device, and can accurately control every detail of the data flow. This characteristic makes it:

  • Ideal for disk cloning and backup
  • Key tools in data recovery process
  • Effective means of performance testing and benchmark measurement
  • Flexible solutions for low-level file operations

Basic command format

The basic syntax structure of the dd command is:

dd if=<Enter a file> of=<Output file> [Option parameters]

in:

  • if(input file): Specify the input source, which can be a device file or a normal file
  • of(output file): Specify the output target, which can also be a device or a file
  • Option parameters: control various characteristics of data transmission

Detailed explanation of core parameters

Input and output control

parameter illustrate Example
if Enter a file if=/dev/sda
of Output file of=
bs Block size bs=4M
count Number of copy blocks count=1024
skip Skip the number of blocks to start the input skip=10
seek Skip the number of blocks at the beginning of the output seek=10

Data conversion options

parameter Function Typical Applications
conv Conversion Options conv=notrunc,noerror
status Progress display status=progress

Practical scenario examples

1. Disk/partition backup and recovery

Create a disk image:

dd if=/dev/sda of=/backup/ bs=4M status=progress

This command backs up the entire sda ​​disk as a mirror file, sets the block size by bs parameter to improve efficiency, and status displays progress.

Recover disk image:

dd if=/backup/ of=/dev/sda bs=4M status=progress

Note: This operation will overwrite all data on the target disk!

2. Create bootable USB

dd if= of=/dev/sdb bs=4M conv=fsync status=progress

Write the ISO image directly to the USB device (SDB) and create a boot disk.

3. Disk performance testing

dd if=/dev/zero of=/tmp/testfile bs=1G count=1 oflag=direct

Test write speed, oflag=direct Bypass cache to obtain real disk performance.

4. Secure data erasure

dd if=/dev/urandom of=/dev/sdX bs=1M status=progress

Cover the entire device with random data to achieve secure erasing.

Advanced application skills

Transfer data from the network

Combined with netcat to implement remote disk cloning:

Receiver:

nc -l 8888 | dd of=/dev/sdb

Sender:

dd if=/dev/sda | nc receiver_ip 8888

Real-time compression backup

dd if=/dev/sda | gzip -c | split -b 2G - .

Create a compressed volume backup that is suitable for large disk backups to multiple small files.

Accurate recovery of partition tables

dd if=/dev/sda of= bs=512 count=1

Backup the MBR partition table (first 512 bytes).

dd if= of=/dev/sda bs=512 count=1

Restore partition tables without affecting partition data.

Risks and precautions

  1. Target device confirmation: Error of parameters may cause catastrophic data loss
  2. Permission Requirements: Usually root permission is required to operate device files
  3. Performance impact: Large capacity disk operation may take a long time
  4. Resource consumption: Large amounts of I/O operations may affect system performance

Safety operation suggestions:

  • Use before operationlsblkConfirm the device identification
  • Verify the command in the test environment first
  • Consider usingconv=noerror,syncHandle bad blocks
  • Make backups of key data in advance

Performance optimization strategy

  1. Block size resize: Optimize bs parameters according to hardware characteristics (usually 4M-8M is better)
  2. Parallel processing: Use PV tools to monitor progress:
dd if=/dev/sda | pv -s $(blockdev --getsize64 /dev/sda) | dd of=/dev/sdb
  • Bypassing cache: Use of lag=direct for performance tests
  • Speed ​​limit control: Limit the transmission rate through pv:
dd if=/dev/sda | pv -L 10m | dd of=/dev/sdb

Alternatives comparison

Although dd is powerful, alternative tools can be considered in some scenarios:

tool Advantages Disadvantages
cp Simple file copying Cannot process device files
rsync Incremental backup, network transmission Not suitable for low-level operations
cat Simple data stream processing Lack of fine control
pv Progress display, speed limit Single function

Typical problems solved

1. Handle bad disks

dd if=/dev/sda of=/dev/sdb conv=noerror,sync

noerror Skip the error, sync fills blocks that cannot be read with NULL.

2. Extract file fragments

dd if= of= bs=1M skip=100 count=10

Extract 10MB of data starting at the 100MB position.

3. Modify the binary file

echo -n "NEWDATA" | dd of= bs=1 seek=100 conv=notrunc

Write new data at file offset 100 bytes without truncating the original file.

The above is the detailed content of the in-depth analysis and application practice of Linux dd command. For more information about Linux dd command application, please pay attention to my other related articles!