dd copies data block by block from an input to an output. It works with ordinary files and with device nodes, which makes it useful and dangerous. It does not ask for confirmation.
The basic form
dd uses name=value operands instead of the usual options.
dd if=/dev/zero of=test.img bs=1M count=1010+0 records in
10+0 records out
10485760 bytes (10 MB, 10 MiB) copied, 0.0123 s, 852 MB/sif=is the input file. Without it,ddreads standard input.of=is the output file. Without it,ddwrites to standard output.bs=is the block size. The default is 512 bytes.count=limits how many input blocks are copied.
Here 10 blocks of 1 MiB make a 10 MiB file. In 10+0, the first number counts full blocks and the second counts partial blocks. Your time and speed will differ. The examples use GNU dd; BSD versions differ in some details.
Check the target twice
Whatever you name in of= is overwritten. Swapping if= and of=, or picking the wrong disk letter, can destroy data in seconds. Run lsblk first, and make sure the target is not mounted.
Common jobs
Save the first sector of a disk, where an MBR partition table lives:
sudo dd if=/dev/sda of=sda-first-sector.bin bs=512 count=1This is not a full backup of a GPT disk, which keeps its partition table in more sectors.
Write a bootable installer image to a USB stick, replacing /dev/sdX with the whole stick:
sudo dd if=installer.iso of=/dev/sdX bs=4M status=progress conv=fsyncstatus=progress shows a running count. conv=fsync makes dd flush the output data to the device before it finishes. Writing an image this way only boots if the image was built for it.
Parts of a file
skip= jumps over blocks in the input, and seek= jumps over blocks in the output.
printf 'abcdefgh' > letters
dd if=letters bs=2 skip=1 count=2 status=none; echocdefBy default dd truncates an existing output file. Use conv=notrunc to change part of a file and keep the rest.
When reading from a pipe, a read can return less than a full block. With count=, add iflag=fullblock so you get the amount you expect.
For everyday file copies, use cp. The files and links lab covers it.
Try it
Work only with ordinary files in a scratch directory.
- Create
test.imgwith the first command and check it withls -lh. - Copy it with
dd if=test.img of=copy.img bs=1M. - Run
cmp test.img copy.img. No output means they are identical. - Run the
lettersexample and changeskipto see what is printed.
Check your understanding
Copying data with dd
All 5 sections ยท no timer
Unlimited retries
Questions from every section are combined into one assessment. Answer them all correctly to complete the article.
Your completion saves in this browser. Sign in to sync it to your account.