← Full reference

dd

Byte-level block copying — files of a given size, cutting out bytes, disk images

dd copies data from one place to another in blocks of a given size and can skip blocks on input and output. Hence its uses: create a file of the right size from /dev/zero or /dev/urandom, cut a piece out of a file at an offset, write bytes into the middle of a file, take or write a disk image. The sandbox has GNU coreutils 9.4.

Syntax

dd if=input of=output bs=block_size count=number_of_blocks [operands…]

Operands are written as name=value, without dashes. Without if= stdin is read; without of= stdout is written. The report (records in/out, speed) goes to stderr.

Operands

OperandWhat it does
if=filewhere to read from (file, device, /dev/zero, /dev/urandom)
of=filewhere to write. An existing file is truncated by default
bs=Nblock size for both reading and writing (default 512 bytes)
ibs=N / obs=Nblock size for reading / writing separately
count=Ncopy only N input blocks
skip=N (iseek=N)skip N blocks on input (of size ibs)
seek=N (oseek=N)skip N blocks on output (of size obs) — start writing there
conv=listcomma-separated conversions (table below)
iflag= / oflag=comma-separated read / write flags
status=LEVELnone — stay silent; noxfer — no final speed line; progress — show progress as it goes

Number suffixes: c = 1, w = 2, b = 512, K/M/G — powers of 1024, kB/MB/GB — powers of 1000. They can be multiplied: bs=1x2K = 2048.

conv=

ValueWhat it does
notruncdon't truncate the output file — needed to overwrite a piece inside a file
fsync / fdatasyncflush data to disk before finishing (honest timing for benchmarks)
sparsemake "holes" instead of writing blocks of zeros
exclfail if the output file already exists
nocreatdon't create the output file
noerrordon't stop on read errors (a failing disk); usually together with sync
syncpad every input block with zeros up to ibs
ucase / lcaseconvert to upper / lower case
swabswap the bytes in every pair

iflag= / oflag=

FlagWhat it does
fullblock (iflag)keep reading until the block is full — required when reading from a pipe
skip_bytes / count_bytes (iflag)count skip= and count= in bytes, not blocks
seek_bytes (oflag)count seek= in bytes
append (oflag)append to the end (together with conv=notrunc)
directdirect I/O, bypassing the cache
sync / dsyncsynchronous write of every block
nofollowdon't open symbolic links

The report

10+0 records in
10+0 records out
10485760 bytes (10 MB, 10 MiB) copied, 0.0137 s, 767 MB/s

10+0 — 10 full blocks and 0 partial ones. 0+2 means blocks were read in "scraps" — a sign of reading from a pipe without iflag=fullblock.

Examples

# a file of exactly 10 MiB of zeros
dd if=/dev/zero of=zero.bin bs=1M count=10

# 4 KiB of random data, no report
dd if=/dev/urandom of=random.bin bs=1K count=4 status=none

# a 100 MiB sparse file without writing data (like truncate -s)
dd if=/dev/zero of=sparse.img bs=1 count=0 seek=100M

# bytes 3 through 7 (offset 3, length 5)
printf '0123456789ABCDEF' > s.txt
dd if=s.txt iflag=skip_bytes,count_bytes skip=3 count=5 status=none   # 34567

# the second 4-byte block
dd if=s.txt bs=4 skip=1 count=1 status=none   # 4567

# overwrite 2 bytes at offset 2 without truncating the file
printf 'XY' | dd of=s.txt bs=1 seek=2 conv=notrunc status=none   # 01XY456789ABCDEF

# the first 512 bytes of a disk/image (MBR) to a file
dd if=disk.img of=mbr.bin bs=512 count=1

# from a pipe — only with fullblock, otherwise the size will be smaller
cat /dev/urandom | dd of=r.bin bs=1M count=2 iflag=fullblock

# a rough disk write speed test
dd if=/dev/zero of=test.bin bs=1M count=100 conv=fsync

# write an image to a USB stick with progress (check the device name first!)
dd if=image.iso of=/dev/sdX bs=4M status=progress conv=fsync

Pitfalls

  • of= mixed up with if=, or the wrong disk given — data is wiped without asking and with no trash can. Before writing to a device, check it with lsblk.
  • Without conv=notrunc the file is truncated. printf 'XY' | dd of=f bs=1 seek=2 leaves 4 bytes in the file (01XY) instead of replacing 2 bytes in the middle.
  • Reading from a pipe without iflag=fullblock. cat /dev/urandom | dd bs=1M count=2 writes not 2 MiB but however much arrived in two reads (128 KiB in the sandbox) and warns "partial read".
  • skip and seek are in blocks, not bytes. bs=1M skip=3 skips 3 MiB. For bytes — iflag=skip_bytes / oflag=seek_bytes or bs=1 (very slow on large volumes).
  • The default bs is 512 bytes: without bs= copying a large file is noticeably slower. bs=1M–4M is sensible.
  • The report goes to stderr. dd … > log won't capture it; use 2> log or status=none.
  • Without conv=fsync the speed is inflated: the data is still in the cache, not on disk.
  • For a file of a given size truncate or fallocate are faster; head -c 1M /dev/zero > f is simpler if you don't need dd.

See also

truncate · split · xxd · head-tail · checksums · filesystems · df-du