# dd — Copy, Clone and Wipe Drives Block by Block

> Practical guide to dd — create disk images, clone drives, write ISOs to USB, wipe disks and copy raw block data, with the safety notes this tool demands.

Source: https://www.jpkc.com/db/en/cheatsheets/shell-system/dd/

<!-- PROSE:intro -->
dd copies data block by block straight at the device level — perfect for disk images, cloning whole drives, building bootable USB sticks or securely wiping disks. That same power makes it dangerous: dd has earned the nickname "disk destroyer" for good reason. Swap `if=` and `of=`, or point `of=/dev/sdX` at the wrong disk, and you overwrite an entire drive instantly and irreversibly — no prompt, no trash can, no undo. Always confirm the target device with `lsblk` before you press Enter.
<!-- PROSE:intro:end -->

## Basic Usage

`dd if=<input> of=<output>` — Copy data from input file to output file.

```bash
dd if=/dev/sda of=/dev/sdb
```

`dd if=<input> of=<output> bs=<size>` — Copy with a specific block size (affects speed).

```bash
dd if=/dev/sda of=disk.img bs=4M
```

`dd if=<input> of=<output> bs=<size> count=<n>` — Copy only N blocks of the specified size.

```bash
dd if=/dev/zero of=testfile bs=1M count=100
```

`dd if=<input> of=<output> status=progress` — Show transfer progress during copy.

```bash
dd if=/dev/sda of=backup.img bs=4M status=progress
```

`dd if=<input> of=<output> conv=<flags>` — Apply conversion options during copy.

```bash
dd if=data.txt of=upper.txt conv=ucase
```

## Disk Images & Cloning

`dd if=<device> of=<image> bs=4M status=progress` — Create a raw disk image from a device.

```bash
dd if=/dev/sda of=disk-backup.img bs=4M status=progress
```

`dd if=<image> of=<device> bs=4M status=progress` — Restore a disk image to a device.

```bash
dd if=disk-backup.img of=/dev/sda bs=4M status=progress
```

`dd if=<device> bs=4M status=progress | gzip > <image>.gz` — Create a compressed disk image.

```bash
dd if=/dev/sda bs=4M status=progress | gzip > disk-backup.img.gz
```

`gunzip -c <image>.gz | dd of=<device> bs=4M status=progress` — Restore a compressed disk image.

```bash
gunzip -c disk-backup.img.gz | dd of=/dev/sda bs=4M status=progress
```

`dd if=<device1> of=<device2> bs=4M conv=noerror,sync status=progress` — Clone a disk (skip errors, pad with zeros).

```bash
dd if=/dev/sda of=/dev/sdb bs=4M conv=noerror,sync status=progress
```

## Bootable USB & ISO

`dd if=<iso> of=<device> bs=4M status=progress oflag=sync` — Write an ISO image to a USB drive.

```bash
dd if=ubuntu-24.04-desktop-amd64.iso of=/dev/sdb bs=4M status=progress oflag=sync
```

`dd if=<device> of=<iso> bs=4M status=progress` — Create an ISO from a CD/DVD drive.

```bash
dd if=/dev/cdrom of=disc.iso bs=4M status=progress
```

`dd if=/dev/zero of=<device> bs=512 count=1` — Wipe the MBR (first 512 bytes) of a disk.

```bash
dd if=/dev/zero of=/dev/sdb bs=512 count=1
```

## Generate Test Files

`dd if=/dev/zero of=<file> bs=1M count=<n>` — Create a file filled with zeros of a specific size.

```bash
dd if=/dev/zero of=100mb.bin bs=1M count=100
```

`dd if=/dev/urandom of=<file> bs=1M count=<n>` — Create a file filled with random data.

```bash
dd if=/dev/urandom of=random.bin bs=1M count=50
```

`dd if=/dev/zero of=<file> bs=1 count=0 seek=<size>` — Create a sparse file (allocates space without writing).

```bash
dd if=/dev/zero of=sparse.img bs=1 count=0 seek=10G
```

## Wipe & Secure Erase

`dd if=/dev/zero of=<device> bs=4M status=progress` — Wipe a disk with zeros.

```bash
dd if=/dev/zero of=/dev/sdb bs=4M status=progress
```

`dd if=/dev/urandom of=<device> bs=4M status=progress` — Overwrite a disk with random data (more secure).

```bash
dd if=/dev/urandom of=/dev/sdb bs=4M status=progress
```

`dd if=/dev/zero of=<file> bs=4M; rm <file>` — Overwrite a file before deleting it.

```bash
dd if=/dev/zero of=secret.txt bs=4M; rm secret.txt
```

## Benchmark & Performance

`dd if=/dev/zero of=<file> bs=1G count=1 oflag=dsync` — Benchmark write speed (sync after each block).

```bash
dd if=/dev/zero of=/tmp/testfile bs=1G count=1 oflag=dsync
```

`dd if=<file> of=/dev/null bs=1M` — Benchmark read speed (discard output).

```bash
dd if=/tmp/testfile of=/dev/null bs=1M
```

`dd if=/dev/zero of=<file> bs=<size> count=<n> oflag=direct` — Test direct I/O write speed (bypass cache).

```bash
dd if=/dev/zero of=/tmp/testfile bs=4M count=256 oflag=direct
```

## Options & Flags

`bs=<size>` — Block size for read and write (K, M, G suffixes). Larger = faster.

```bash
bs=4M (common for disk operations)
```

`count=<n>` — Copy only N input blocks.

```bash
count=100 (with bs=1M = 100 MB)
```

`skip=<n>` — Skip N blocks at the start of input.

```bash
skip=1 bs=512 (skip MBR)
```

`seek=<n>` — Skip N blocks at the start of output.

```bash
seek=1 bs=512 (write after MBR)
```

`conv=noerror,sync` — Continue on read errors and pad with zeros (for damaged disks).

```bash
dd if=/dev/sda of=rescue.img conv=noerror,sync bs=4K
```

`oflag=sync` — Synchronize output after each write (ensures data is flushed).

```bash
dd if=image.iso of=/dev/sdb bs=4M oflag=sync
```

`iflag=fullblock` — Accumulate full input blocks (prevents short reads from pipes).

```bash
dd if=/dev/stdin of=output.bin bs=1M iflag=fullblock
```

<!-- PROSE:outro -->
## Conclusion

dd is one of the most powerful — and most dangerous — tools in the Unix world: it writes blindly to exactly where you tell it, with no confirmation and no undo. A wrong `of=/dev/sdX` destroys an entire disk in seconds. So **always** verify the target device first with `lsblk` or `fdisk -l`, never swap `if=` and `of=`, and unmount the device before writing. Use `status=progress` for visible progress, a sensible `bs=` (usually `4M`) for throughput, and `conv=fsync` or `oflag=sync` so all data actually lands on the medium instead of sitting in the cache. On macOS, address the raw device `/dev/rdiskN` (much faster) and unmount it first with `diskutil unmountDisk`. When in doubt, check one more time rather than overwriting a disk you can never get back.

## Further Reading

- [Arch Wiki: dd](https://wiki.archlinux.org/title/Dd) – thorough reference covering disk images, cloning and secure erasing
- [Wikipedia: dd (Unix)](https://en.wikipedia.org/wiki/Dd_(Unix)) – background, options and common usage patterns
<!-- PROSE:outro:end -->

## Related Commands

- [lsblk](https://www.jpkc.com/db/en/cheatsheets/shell-system/lsblk/) – list block devices; use it to verify the target before any dd write
- [fdisk](https://www.jpkc.com/db/en/cheatsheets/shell-system/fdisk/) – view and edit partition tables
- [mount](https://www.jpkc.com/db/en/cheatsheets/shell-system/mount/) – mount and unmount filesystems (unmount before dd)

