LinuxCommandLibrary

dd

Copy and convert data

TLDR

Make a bootable USB drive from an isohybrid file (such as archlinux-xxx.iso) and show the progress

$ dd if=[path/to/file.iso] of=[/dev/usb_drive] status=progress
copy

Clone a drive to another drive with 4 MiB block size and flush writes before the command terminates
$ dd bs=4194304 conv=fsync if=[/dev/source_drive] of=[/dev/dest_drive]
copy

Generate a file with a specific number of random bytes by using kernel random driver
$ dd bs=[100] count=[1] if=/dev/urandom of=[path/to/random_file]
copy

Benchmark the sequential write performance of a disk
$ dd bs=[1024] count=[1000000] if=/dev/zero of=[path/to/file_1GB]
copy

Create a system backup, save it into an IMG file (can be restored later by swapping if and of), and show the progress
$ dd if=[/dev/drive_device] of=[path/to/file.img] status=progress
copy

SYNOPSIS

dd [if=file] [of=file] [bs=bytes] [ibs=bytes] [obs=bytes] [count=n] [skip=n] [seek=n] [conv=conv[,conv...]] [status=level] ...

PARAMETERS

if=file
    Input file; default stdin. Use /dev/zero, /dev/sda for devices.

of=file
    Output file; default stdout. Creates/truncates unless conv=notrunc.

bs=bytes
    Block size for both input/output (sets ibs=obs). E.g., 1M, 4k.

ibs=bytes
    Input block size; default 512 bytes.

obs=bytes
    Output block size; default 512 bytes.

count=blocks
    Copy only n input blocks; 0=unlimited.

skip=blocks
    Skip n input blocks before copying.

seek=blocks
    Skip n output blocks (requires of seekable).

conv=conv[,conv...]
    Convert: notrunc(no truncate), noerror(ignore read errors), sync(pad blocks), fsync, sparse, swab, etc.

status=level
    none(no info), noxfer(final stats), progress(live ETA/speed).

cbs=bytes
    Conversion block size for block/unblock.

iflag=flags
    Input open flags: append, direct, dsync, sync, nonblock, etc.

oflag=flags
    Output open flags (see iflag).

DESCRIPTION

dd is a powerful Unix command for copying and converting raw data streams with fine-grained control. It reads from an input source (if, default stdin) in blocks of size ibs or bs, optionally skips or seeks blocks, applies conversions via conv, and writes to an output (of, default stdout) in obs blocks. Use cases include disk cloning, image creation, drive wiping, partition backups, and data recovery.

Key strengths: handles devices directly (/dev/sda), supports sparse files, and precise byte manipulation. However, it lacks safeguards—no prompts, no progress by default (use status=progress)—risking data loss. Options like conv=noerror,sync help with damaged media by ignoring errors and padding blocks.

Block sizes affect performance; multiples of sector size (512 bytes) or page size are optimal. Multipliers: k=1024, M=1024*1024, etc. dd processes until EOF or count blocks, making it ideal for low-level tasks where cp falls short.

CAVEATS

Dangerous: no confirmation; easily destroys data (e.g., of=/dev/sda). Verify paths. Slow without optimal bs. Errors silent without status. Use sudo for devices. Test on files first.

COMMON EXAMPLES

Clone disk: dd if=/dev/sda of=/dev/sdb bs=4M status=progress
Image partition: dd if=/dev/sda1 of=image.img bs=1M
Wipe drive: dd if=/dev/urandom of=/dev/sda status=progress
Recover damaged: dd if=/dev/sda of=image.img conv=noerror,sync

PERFORMANCE TIPS

Use large bs (1M-128M). oflag=direct bypasses cache. Monitor with status=progress. Multicore: dd single-threaded; pair with pv.

HISTORY

Originated in Version 4 Unix (1973) at Bell Labs by Ken Thompson. Evolved through BSD/POSIX; core syntax stable since AT&T System III (1981). GNU coreutils version adds status=progress (2000s). Ubiquitous for imaging/cloning.

SEE ALSO

cp(1), cat(1), pv(1), tee(1), rsync(1)

Copied to clipboard