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
| Operand | What it does |
|---|---|
if=file | where to read from (file, device, /dev/zero, /dev/urandom) |
of=file | where to write. An existing file is truncated by default |
bs=N | block size for both reading and writing (default 512 bytes) |
ibs=N / obs=N | block size for reading / writing separately |
count=N | copy 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=list | comma-separated conversions (table below) |
iflag= / oflag= | comma-separated read / write flags |
status=LEVEL | none — 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=
| Value | What it does |
|---|---|
notrunc | don't truncate the output file — needed to overwrite a piece inside a file |
fsync / fdatasync | flush data to disk before finishing (honest timing for benchmarks) |
sparse | make "holes" instead of writing blocks of zeros |
excl | fail if the output file already exists |
nocreat | don't create the output file |
noerror | don't stop on read errors (a failing disk); usually together with sync |
sync | pad every input block with zeros up to ibs |
ucase / lcase | convert to upper / lower case |
swab | swap the bytes in every pair |
iflag= / oflag=
| Flag | What 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) |
direct | direct I/O, bypassing the cache |
sync / dsync | synchronous write of every block |
nofollow | don'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 withif=, or the wrong disk given — data is wiped without asking and with no trash can. Before writing to a device, check it withlsblk.- Without
conv=notruncthe file is truncated.printf 'XY' | dd of=f bs=1 seek=2leaves 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=2writes not 2 MiB but however much arrived in two reads (128 KiB in the sandbox) and warns "partial read". skipandseekare in blocks, not bytes.bs=1M skip=3skips 3 MiB. For bytes —iflag=skip_bytes/oflag=seek_bytesorbs=1(very slow on large volumes).- The default
bsis 512 bytes: withoutbs=copying a large file is noticeably slower.bs=1M–4Mis sensible. - The report goes to stderr.
dd … > logwon't capture it; use2> logorstatus=none. - Without
conv=fsyncthe speed is inflated: the data is still in the cache, not on disk. - For a file of a given size
truncateorfallocateare faster;head -c 1M /dev/zero > fis simpler if you don't needdd.
See also
truncate · split · xxd · head-tail · checksums · filesystems · df-du