DD Command Examples - Convert, Copy, and Monitor Data

Explore practical DD command examples for data conversion, file copying, and progress monitoring. Learn to use dd for various system administration tasks.

DD Command Examples

The dd command is a powerful Unix utility for converting and copying files. It can be used for various low-level operations, including disk imaging, data backup, and data scrubbing. Below are several practical examples of how to use the dd command for different tasks.

1. Creating Files with Random Data

This example demonstrates how to read random data from /dev/urandom and write it to a file named /tmp/test.txt. It specifies reading 2 blocks of 512 Bytes each.

# Read from {/dev/urandom} 2*512 Bytes and put it into {/tmp/test.txt}
# Note: At the first iteration, we read 512 Bytes.
# Note: At the second iteration, we read 512 Bytes.
dd if=/dev/urandom of=/tmp/test.txt count=2 bs=512

2. Monitoring DD Progress

Monitoring the progress of dd is crucial for long-running operations. Here are several methods to achieve this:

2.1. Using `pgrep` and `kill -USR1`

This method uses pgrep to find the dd process ID and then sends the USR1 signal to display its progress. The loop refreshes the display.

# Watch the progress of 'dd'
dd if=/dev/zero of=/dev/null bs=4KB &; export dd_pid=`pgrep '^dd'`; while [[ -d /proc/$dd_pid ]]; do kill -USR1 $dd_pid && sleep 1 && clear; done

2.2. Using `pv` with `dialog`

This example pipes the output of dd (which is redirected to /dev/null) through pv (Pipe Viewer) and then uses dialog to display a progress bar. You need to install pv and dialog.

# Watch the progress of 'dd' with `pv` and `dialog` (apt-get install pv dialog)
(pv -n /dev/zero | dd of=/dev/null bs=128M conv=notrunc,noerror) 2>&1 | dialog --gauge "Running dd command (cloning), please wait..." 10 70 0

2.3. Using `pv` with `zenity`

Similar to the dialog method, this uses pv but displays the progress using zenity, a graphical dialog box utility. Install pv and zenity.

# Watch the progress of 'dd' with `pv` and `zenity` (apt-get install pv zenity)
(pv -n /dev/zero | dd of=/dev/null bs=128M conv=notrunc,noerror) 2>&1 | zenity --title 'Running dd command (cloning), please wait...' --progress

2.4. Using Built-in `status=progress`

Modern versions of dd (coreutils v8.24 and later) have a built-in status=progress option that simplifies progress monitoring.

# Watch the progress of 'dd' with the built-in `progress` functionality
# (introduced in coreutils v8.24)
dd if=/dev/zero of=/dev/null bs=128M status=progress

3. Using `dcfldd` for Graphical Output

dcfldd is an enhanced version of dd that provides more detailed progress information, often in a more visually appealing format.

# DD with "graphical" return
dcfldd if=/dev/zero of=/dev/null bs=500K

4. Transferring Audio via SSH

This advanced example shows how to capture audio from a microphone (/dev/dsp) and stream it over SSH to another computer's speaker port. Note that the audio quality is typically poor.

# This will output the sound from your microphone port to the ssh target
# computer's speaker port. The sound quality is very bad, so you will hear a
# lot of hissing.
dd if=/dev/dsp | ssh -c arcfour -C username@host dd of=/dev/dsp

5. Creating Sparse Files

A sparse file is a file that contains empty blocks that are not actually stored on disk until they are written to. This example creates a 1MB file with zero allocated blocks.

# Create a 1MB file with zero allocated blocks:
dd if=/dev/zero of=foo1 seek=1 bs=1M count=0

External Resources