dd - Linux


Overview

The dd command (derived from “data duplicator”) in Linux is a powerful utility used for low-level data copying and conversion. Its primary function is to copy and transform raw data from one form to another, often from devices like hard disks, partitions, or files, providing precise control over data handling and transformation. dd is particularly valuable for tasks like cloning disk partitions, creating bootable USB drives, backing up systems, or recovering data from corrupt media.

Syntax

The basic syntax for the dd command is:

dd [OPTIONS]...

The operation of dd typically involves defining an input file (if=) and output file (of=), along with a set of options that control its operation:

dd if=/path/to/input of=/path/to/output [options]

Both if and of parameters can refer to files or device nodes (e.g., /dev/sda).

Options/Flags

  • if=FILE: Read from FILE instead of stdin.
  • of=FILE: Write to FILE instead of stdout.
  • bs=BYTES: Set both input and output block sizes to BYTES. This is often set to optimize device read/write speeds.
  • count=N: Copy only N input blocks.
  • skip=N: Skip N input blocks before starting to copy.
  • seek=N: Skip N output blocks before writing data.
  • conv=CONVERSIONS: Apply one or more comma-separated conversions, like notrunc (do not truncate the output file), sync (pad every input block to size of ibs with null bytes), or noerror (continue operation, ignoring read errors).

Default values are generally system and context-dependent, particularly for block sizes (bs).

Examples

  1. Basic Disk Cloning:

    dd if=/dev/sda of=/dev/sdb bs=4M
    

    Clone an entire disk /dev/sda to another disk /dev/sdb with a block size of 4MB.

  2. Creating a Bootable USB Drive:

    dd if=linux.iso of=/dev/sdc bs=4M status=progress
    

    Write an ISO image to a USB drive, showing progress as it writes.

  3. Creating a Backup of a Partition:

    dd if=/dev/sda1 of=~/partition1.backup
    

    Backup the first partition of disk sda to a file in the user’s home directory.

Common Issues

  • Data Destruction: Accidental use of dd can lead to irreversible data loss. Always double-check if= and of= values.
  • Performance: Using incorrect block sizes (bs=) can significantly affect performance. Experiment with different sizes to optimize speed and reliability.
  • Device Confusion: Misidentifying device names (e.g., /dev/sda vs. /dev/sdb) can result in writing to the wrong device. Always confirm device names with lsblk or similar commands.

Integration

dd is commonly used in scripts and pipelines involving disk management. For example, after creating a disk image, you might compress it with gzip:

dd if=/dev/sda bs=4M | gzip > backup.gz

This command creates a compressed backup of the entire sda device.

  • cp: For copying files within the filesystem, with more user-friendly features.
  • rsync: Used for backing up and syncing files, offering more flexibility than dd.
  • fdisk: Disk partitioning tool that can be checked before using dd to verify disk structures.

For further reading and detailed documentation, visit the GNU Coreutils page for dd.