dd

disk utilitiesLinux/Unix
The dd command is one of the most frequently used commands in Linux/Unix-like operating systems. dd The dd command is a powerful utility for converting and copying files with the ability to manipulate the data as it is being copied. It's often used for tasks such as backing up boot sectors, creating disk images, and performing low-level operations on storage devices.

Quick Reference

Command Name:

dd

Category:

disk utilities

Platform:

Linux/Unix

Basic Usage:

dd [options] [arguments]

Common Use Cases

  • 1

    Create disk images

    Make exact copies of disks or partitions for backup or cloning

  • 2

    Convert data formats

    Transform data between different formats while copying

  • 3

    Backup & restore

    Backup MBR, partition tables, and other critical disk areas

  • 4

    Disk benchmarking

    Test read/write performance of storage devices

Syntax

dd [OPERAND]...

Options

Operand Description
if=FILE Read from FILE instead of stdin
of=FILE Write to FILE instead of stdout
bs=BYTES Read and write up to BYTES bytes at a time (default: 512); overrides ibs and obs
ibs=BYTES Read up to BYTES bytes at a time
obs=BYTES Write BYTES bytes at a time
count=N Copy only N input blocks
skip=N Skip N input blocks before starting to copy
seek=N Skip N output blocks before starting to write
conv=CONVS Convert the file according to the comma-separated list of conversions
status=LEVEL The level of information to print (none, noxfer, progress)
iflag=FLAGS Read as per the comma-separated list of flags
oflag=FLAGS Write as per the comma-separated list of flags

Conversion Options (for conv=):

Option Description
ascii Convert EBCDIC to ASCII
ebcdic Convert ASCII to EBCDIC
lcase Change uppercase to lowercase
ucase Change lowercase to uppercase
sparse Try to seek rather than write all-NUL output blocks
sync Pad every input block with NULs to ibs size
fdatasync Physically write output file data before finishing
fsync Physically write output file data and metadata before finishing
noerror Continue after read errors
notrunc Do not truncate the output file

Examples

How to Use These Examples

The examples below show common ways to use the dd command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

#

Basic Examples:

dd if=/dev/zero of=file.txt count=100 bs=1k

Create a 100KB file filled with zeros.

dd if=/dev/sda of=disk.img

Create an exact image of the /dev/sda disk.

dd if=source.file of=dest.file bs=1M

Copy source.file to dest.file using 1MB blocks.

Advanced Examples:

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

Clone one disk to another with progress indication.

dd if=/dev/urandom of=random.bin bs=1M count=10

Create a 10MB file filled with random data.

dd if=file.img of=/dev/sdc bs=4M conv=fdatasync status=progress

Write an image file to a USB device, ensuring data is physically written.

dd if=/dev/sda of=mbr.bin bs=512 count=1

Backup the Master Boot Record (first 512 bytes of the disk).

dd if=/dev/zero of=/dev/sdb bs=1M count=100

Wipe the first 100MB of a disk with zeros.

dd if=input.file of=output.file bs=1k skip=1k count=10k

Extract 10MB from input.file, skipping the first 1MB.

dd if=/dev/sda | gzip > disk.img.gz

Create a compressed disk image.

Try It Yourself

Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

Understanding Syntax

Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

Notes

Key Points:

  • The name dd comes from IBM JCL's DD (Data Definition) statement
  • Unlike most Unix commands, dd uses a unique syntax with keyword=value pairs instead of traditional options
  • dd is particularly powerful for low-level operations because it can access devices directly
  • Use caution when working with dd as incorrect usage can lead to data loss
  • For improved performance, larger block sizes (bs) are generally more efficient
  • The conv=fdatasync/fsync options are important for ensuring data is fully written to physical storage

Common Use Cases:

  • Creating disk images or exact copies of storage devices
  • Writing disk images to USB drives or SD cards
  • Backing up and restoring partition tables or boot sectors
  • Benchmarking disk I/O performance
  • Securely wiping disks with random data or zeros
  • Converting data between different formats
  • Creating files of specific sizes for testing purposes

Performance Considerations:

  • Block size (bs) significantly affects performance - larger blocks typically yield better throughput
  • Common block sizes are 1k, 4k, 64k, 1M, 4M, and 8M
  • The optimal block size depends on hardware, especially buffer sizes and I/O capabilities
  • Using status=progress shows real-time transfer statistics
  • For large transfers, consider adding the conv=fdatasync option to ensure all data is physically written

Safety Precautions:

  • Always double-check device names (especially when using of=/dev/sdX)
  • Consider using device-by-id or UUID paths instead of /dev/sdX which can change
  • Running dd with insufficient privileges will result in permission errors when accessing devices
  • When cloning drives, ensure the destination drive is at least as large as the source
  • Using count and skip limits the potential damage if an error occurs

Related Commands:

  • cp - For regular file copying needs
  • cat - For simpler concatenation tasks
  • dcfldd - An enhanced version of dd with additional features
  • ddrescue - A data recovery tool that builds on dd's capabilities
  • pv - Pipe Viewer, useful for monitoring the progress of data through a pipeline

Tips & Tricks

1

Monitor progress with status=progress: dd if=/dev/sda of=/dev/sdb bs=4M status=progress

2

Use conv=sync,noerror to continue after read errors: dd if=/dev/sda of=disk.img bs=4M conv=sync,noerror

3

Add signal handling to check progress: kill -USR1 $(pgrep dd) shows progress without interrupting

4

Use pipe viewer for better progress monitoring: dd if=/dev/sda | pv | dd of=/dev/sdb

5

Optimize performance with appropriate block size (bs): 4M or 8M often works well for modern systems

6

Use conv=fdatasync to ensure data is physically written: dd if=file.img of=/dev/sdb bs=4M conv=fdatasync status=progress

7

Create sparse files to save space: dd if=/dev/zero of=sparse.img bs=1 count=0 seek=10G creates a 10GB sparse file

Common Use Cases

Create disk images

Make exact copies of disks or partitions for backup or cloning

Convert data formats

Transform data between different formats while copying

Backup & restore

Backup MBR, partition tables, and other critical disk areas

Disk benchmarking

Test read/write performance of storage devices

Secure data wiping

Overwrite disks with zeros or random data for secure erasure

Related Commands

cp

cp

View command

cat

cat

View command

dcfldd

dcfldd

View command

ddrescue

ddrescue

View command

pv

pv

View command

tar

tar

View command

gzip

gzip

View command

Learn By Doing

The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the dd command works in different scenarios.

$ dd
View All Commands