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

    Data duplication

    Copy and convert data between files or devices

  • 2

    Disk cloning

    Create exact copies of disks or partitions

  • 3

    Data recovery

    Recover data from damaged or corrupted disks

  • 4

    Scripting

    Use in shell scripts to automate data duplication

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

Use the -i input_blocks option to specify the number of input blocks

2

Use the -o output_blocks option to specify the number of output blocks

3

Use the -f file option to specify the input file

4

Use the -s option to skip over input blocks

5

Use the -c option to count input blocks

Common Use Cases

Data duplication

Copy and convert data between files or devices

Disk cloning

Create exact copies of disks or partitions

Data recovery

Recover data from damaged or corrupted disks

Scripting

Use in shell scripts to automate data duplication

Data manipulation

Manipulate and transform data streams

Related Commands

These commands are frequently used alongside dd or serve similar purposes:

Use Cases

1

Data duplication

Copy and convert data between files or devices

2

Disk cloning

Create exact copies of disks or partitions

3

Data recovery

Recover data from damaged or corrupted disks

4

Scripting

Use in shell scripts to automate data duplication

5

Data manipulation

Manipulate and transform data streams

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