Skip to content

Linux Foundations / Understand the system underneath ยท Step 19 of 38

Lesson: Copying data with ddSwitch
โ† Linux Foundations

System administration

Copying data with dd

Use dd safely to copy blocks of data, create test files and back up parts of a disk.

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.

bash
dd if=/dev/zero of=test.img bs=1M count=10
10+0 records in
10+0 records out
10485760 bytes (10 MB, 10 MiB) copied, 0.0123 s, 852 MB/s
  • if= is the input file. Without it, dd reads standard input.
  • of= is the output file. Without it, dd writes 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:

bash
sudo dd if=/dev/sda of=sda-first-sector.bin bs=512 count=1

This 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:

bash
sudo dd if=installer.iso of=/dev/sdX bs=4M status=progress conv=fsync

status=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.

bash
printf 'abcdefgh' > letters
dd if=letters bs=2 skip=1 count=2 status=none; echo
cdef

By 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.

  1. Create test.img with the first command and check it with ls -lh.
  2. Copy it with dd if=test.img of=copy.img bs=1M.
  3. Run cmp test.img copy.img. No output means they are identical.
  4. Run the letters example and change skip to 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.