dd Command - File Copy and Conversion Utility
The dd command is a powerful and versatile utility in Unix-like operating systems, often referred to as the "disk duplicator" or "convert and copy a file" tool. It is primarily used for low-level copying and conversion of data. Despite its simple syntax, dd can perform complex operations such as disk imaging, data wiping, and format conversions.
Understanding dd Command Basics
The fundamental syntax of the dd command involves specifying an input file (if) and an output file (of). It operates on blocks of data, allowing for precise control over the size of these blocks (bs) and the number of blocks to process (count).
dd Command Examples and Use Cases
Creating Files with Specific Content
You can use dd to create files filled with specific data, such as random bytes or zeros. This is useful for testing or preparing storage devices.
# dd
# Convert and copy a file (AKA: Destroyer of Disks)
# Read from `/dev/urandom`, 2*512 Bytes, and put it into `/tmp/test.txt`.
# Note: each iteration reads 512 bytes (the selected block size).
dd if=/dev/urandom of=/tmp/test.txt count=2 bs=512
Monitoring dd Command Progress
Monitoring the progress of long-running dd operations is crucial. Several methods can be employed, including using signals or external tools like pv.
# Watch the progress of dd(1).
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
# Watch the progress of dd(1) with pv(1) and dialog(1), both of which can be
# installed with the following command: 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
# Watch the progress of dd(1) with pv(1) and zenity(1), both of which can be
# installed with the following command: apt-get install pv zenity
(
pv -n /dev/zero | dd of=/dev/null bs=128M conv=notrunc,noerror
) 2>&1 | zenity --title 'Cloning with dd(1) -- please wait...' --progress
# Watch the progress of dd(1) with the built-in `progress` functionality, -
# introduced in CoreUtils v8.24.
dd if=/dev/zero of=/dev/null bs=128M status=progress
# DD with "graphical" return
dcfldd if=/dev/zero of=/dev/null bs=500K
# Show current progress without interruption (USR1)
dd if=/dev/zero of=/dev/null & pid=$!
kill -USR1 $pid
Advanced dd Command Operations
dd can also be used for more advanced tasks, such as redirecting audio streams or preparing swap files.
# 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
# Create a 1GiB file with nothing but zeros, ready to mkswap(8) it.
dd if=/dev/zero of=/swapfile count=1048576 bs=1024 status=progress