SoFunction
Updated on 2025-03-10

How to copy and convert data using the dd command in Linux

Introduction

LinuxIn-houseddCommands are a powerful data copy and conversion utility. It runs at a lower level and is usually used to create bootableUSBTasks such as drives, cloning disks, and generating random data.

ddThe full name can be:data duplicatordisk destroyerandData Definition

Functions and capabilities

  • Disk Image: Creates an exact, bit-by-bit copy of the entire disk or partition

  • Data erasing: Safely overwrite the drive with zero or random data

  • File conversion: Conversion, byte order exchange and file fill between ASCII and EBCDIC

  • Data recovery: Read data from a failed drive by ignoring read errors

  • Start media creation: Write the disk image to a USB drive or SD card

  • Storage performance testing: A rough benchmark test for drive write speed

grammar

dd if=<input_file> of=<output_file> [options]
  • if: Input file (source file or device, e.g./dev/sda/dev/zero

  • of: Output file (target file or device, for example,/dev/sdb

  • Options: Customized behavior options

Common options

  • bs=[BYTES]: Set both input and output block sizes toBYTES

Block size representationddThe data size that the command reads or writes at one time each input or output

  • count=[N]: Copy onlyNInput blocks

  • skip=[N]: Skip the input file before starting copyingNA block

  • seek=[N]: Skip the output file before starting writingNA block

  • conv=[TYPE]: Specify the conversion type (for example,sync、noerror、notrunc

  • status=[LEVEL]: Control the output verbosity (for example,none、 noxfer、 progress

  • iflag=[FLAGS]: Enter a specific flag (direct、sync

  • oflag=[FLAGS]: Output specific flags (append、sync

  • ibs: Set the input block size

  • obs: Set the output block size

  • noerror: Continue after reading error

  • notrunc: Do not truncate the output file

  • sync:useNULLFill each input block toibssize

Example usage

Basic usage

dd if= of=

# If the target file does not exist, it will be created automatically, otherwise the target file will be overwritten.

Create a bootable USB drive

WillISOFile writingUSBDrive

sudo dd if= of=/dev/sdb bs=4M status=progress
  • if=: InputISOdocument

  • of=/dev/sdb: OutputUSBequipment

  • bs=4M: Use 4 MB block size to speed up copying

  • status=progress: Display progress during operation

Backup disk

Create a disk image

sudo dd if=/dev/sda of= bs=64K conv=sync,noerror
  • if=/dev/sda: The input original disk device

  • of=: Output disk image

  • bs=64K: Block size is 64 KB

  • conv=sync,noerror: Continue reading when an error occurs and fill it with control

Recover disk from mirror

sudo dd if= of=/dev/sda bs=64K

Create a file containing random data

dd if=/dev/urandom of=random_data.bin bs=1M count=10
  • if=/dev/urandom: Random input source

  • of=random_data.bin: The output file

  • bs=1M: Block size is 1 MB

  • count=10: Create a 10 MB file

Securely erase disk

Overwrite disk with random data

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

Test disk write speed

Write zeros to disk to test write speed

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

Split the file into chunks

Split the file into smaller chunks

dd if=largefile of=smallfile bs=1M count=100

Prevent overwriting of target files

dd if= of= conv=notrunc

Append data to file

dd if= of= conv=append

Compress the data read by dd

sudo dd if=/dev/sda bs=1M | gzip -c -9 > 

Display progress bar during operation

dd if=source_file of=destination_file status=progress

Convert file data format from EBCDIC to ASCII

sudo dd if= of= conv=ascii

Key conversion flags

  • sync: Fill each block with empty bytes to reach the specified size

  • noerror: Even though there is a read error, continue operation

  • notrunc: Do not truncate the output file

  • ucase: Convert text to capital

  • lcase: Convert text to lowercase

The above is the detailed information about how Linux uses the dd command to copy and convert data. For more information about copying and converting data from the Linux dd command, please pay attention to my other related articles!