Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

dd Command Guide for Converting and Copying Raw Data

dd copies data block by block between files and devices, converting as it goes — the tool for disk images, bootable USB sticks, and low-level backups, and the one command most likely to destroy your data by accident.


What is dd, and Why Use It?

dd reads raw data from an input (if=) and writes it to an output (of=), optionally converting along the way. It doesn’t understand “files” the way your shell does — it sees streams of bytes — which is exactly why it can image an entire disk, write an ISO to a USB stick, or clone a partition byte for byte.

dd answers the question “can I get these exact bytes from here to there?” — with a large warning label attached.

Why Learn dd?

  • Writing disk images: burning an OS ISO to a USB drive or SD card.
  • Imaging whole disks or partitions for backups, forensics, or recovery.
  • Cloning a disk onto an equal-or-larger replacement.
  • Benchmarking raw throughput of disks and networks.

dd does not ask questions, and it has no undo. Swap if= and of= and it will happily overwrite your home directory with zeros. Re-read the command line before every run, and use status=progress so you can see what’s happening.


1. Copy a File Block by Block

dd if=source.iso of=copy.iso bs=4M
  • if= is the input file, of= is the output file, and bs= sets the block size. Bigger blocks mean fewer read/write calls and much faster copies — 4M is a good default for images.

Example:

dd if=notes.txt of=notes-backup.txt bs=1M

2. Write a Disk Image to a USB Drive or SD Card

lsblk
dd if=ubuntu-24.04.iso of=/dev/sdb bs=4M status=progress oflag=sync
  • lsblk (or diskutil list on macOS) shows device names — the USB stick is usually /dev/sdb or /dev/sdc on Linux, /dev/disk4 on macOS.
  • Point of= at the whole device (/dev/sdb), not a partition (/dev/sdb1) — writing to a partition corrupts the partition table.
  • status=progress prints live progress; oflag=sync flushes all writes before you pull the stick.
  • One wrong letter here is irreversible. Triple-check the device before pressing Enter.

3. Image an Entire Disk or Partition

sudo dd if=/dev/sda of=backup-sda.img bs=16M status=progress
  • sudo is required to read raw devices.
  • The result is a byte-for-byte image you can write back later, or mount with a loop device to inspect.
  • Use if=/dev/sda1 to image a single partition instead of the whole disk.
  • Imaging a mounted, in-use filesystem produces an inconsistent snapshot — unmount it, or boot from a live USB first.

4. Clone a Disk to Another Disk

sudo dd if=/dev/nvme0n1 of=/dev/nvme1n1 bs=16M status=progress conv=fsync
  • Device-to-device copy: the target must be at least as large as the source’s used area.
  • conv=fsync forces a full flush before dd exits, so the clone is complete when the prompt returns.
  • This overwrites everything on the target — there is no confirmation prompt.

5. Copy Only Part of a File

dd if=big.iso of=chunk.iso bs=1M skip=100 count=50
  • skip=N skips the first N input blocks; count=N stops after N blocks. With bs=1M this copies the 100–150 MB range.
  • Pair with seek=N when writing into an existing file at an offset.

6. Zero Out a Disk or File

dd if=/dev/zero of=/dev/sdX bs=16M status=progress
  • /dev/zero is an endless stream of zero bytes; writing it to a device erases everything on it.
  • This destroys all data on the target with no recovery path. Re-read the of= path twice.
  • A smaller variant is useful for creating test files: dd if=/dev/zero of=sparse.img bs=1M count=100 makes a 100 MB file of zeros.

7. Test Raw Disk Throughput

dd if=/dev/zero of=/tmp/testfile bs=1M count=1024 oflag=direct
  • Writes 1 GiB of zeros and prints the achieved speed in the summary line (which goes to stderr). oflag=direct bypasses the cache so the number reflects the real device, not RAM.
  • To test reads instead: dd if=/dev/sda of=/dev/null bs=1M count=1024 iflag=direct.

8. Verify What dd Wrote

cmp original.iso copy.iso
  • cmp stops at the first differing byte; md5sum or sha256sum compare whole-file hashes. For images, this is the difference between “I think it worked” and “I know it worked”.

Common Pitfalls

  • Swapping if= and of= is the classic fatal mistake. dd will overwrite the input with the output without complaint. Re-read the command before running it.
  • Device vs. partition. /dev/sdb is the whole disk; /dev/sdb1 is one partition. Writing an image to the wrong one can destroy the partition table.
  • Reported speeds include caching. Without oflag=direct / iflag=direct (or conv=fsync), dd can report gigabytes per second that are really just RAM.
  • Failing disks need conv=noerror,sync. noerror keeps going past read errors; sync pads the short reads so block offsets stay aligned.
  • macOS and BSD dd differ slightly: block suffixes are lowercase (bs=4m), and GNU-only flags like status=progress don’t exist there.

CommandDescription
cmpVerify the copy byte by byte
scpNetwork copies when dd’s raw approach isn’t needed
duCheck how big an image is growing

dd Cheatsheet

CommandWhat It Does
dd if=in of=out bs=4MCopy a file block by block
dd if=image.iso of=/dev/sdX bs=4M status=progressWrite an ISO to a USB stick
sudo dd if=/dev/sda of=backup.img bs=16MImage a whole disk
sudo dd if=/dev/sda of=/dev/sdb bs=16M conv=fsyncClone one disk to another
dd if=in of=out bs=1M skip=100 count=50Copy a slice of a file
dd if=/dev/zero of=f bs=1M count=100Create a 100 MB zero-filled file
dd if=/dev/zero of=/dev/sdX bs=16MWipe a device (destroys everything)
dd if=/dev/zero of=t bs=1M count=1024 oflag=directBenchmark raw write speed
dd if=/dev/sda of=/dev/null iflag=direct bs=1MBenchmark raw read speed