Overview
The dd command is a tool used for low-level copying and conversion of data between files and devices using specified block sizes.
Unlike the standard cp command, dd can directly read and write to device files (e.g., /dev/sda). This makes it essential for system administration tasks such as full disk backups, creating OS installation USBs, and generating dummy data.
Due to its power—where a single mistake in the output path can instantly destroy a system—it is also infamously known as the “Disk Destroyer.” It is a command that requires extreme caution during operation.
Specifications (Arguments and Options)
Syntax
dd [operand=value] ...
Unlike most Unix commands, dd uses the operand=value format for its arguments.
Main Operands
| Operand | Description |
if=FILE | Specifies the Input File. Can be a file or a device (e.g., /dev/zero, /dev/sda). |
of=FILE | Specifies the Output File. Can be a file or a device. |
bs=BYTES | Specifies the Block Size for reading and writing at once (e.g., 1M, 4M). Sets both ibs and obs. |
ibs=BYTES | Specifies the input block size. |
obs=BYTES | Specifies the output block size. |
count=N | Specifies the number of blocks to copy. Total size = bs × count. Copies until the end if omitted. |
conv=CONVS | Specifies data conversion options (multiple options can be comma-separated). |
status=LEVEL | Specifies the level of status information to display (e.g., progress for real-time updates). |
Main conv Option Values
| Value | Description |
noerror | Continues operation even if read errors occur (essential for data recovery). |
sync | Pads every input block with zeros to match the ibs size if it is smaller. |
notrunc | Does not truncate the output file; only overwrites the parts where data is present. |
ucase | Converts lowercase letters to uppercase. |
lcase | Converts uppercase letters to lowercase. |
Basic Usage
Creating a Empty File (Dummy Data)
This is a standard method for creating a file of a specific size for testing. It uses /dev/zero (a device that produces infinite null characters) as the input.
# Write 100 blocks of 1MB to create a 100MB file
dd if=/dev/zero of=testfile_100m.img bs=1M count=100
Execution Result Example:
100+0 records in
100+0 records out
104857600 bytes (105 MB, 100 MiB) copied, 0.052341 s, 2.0 GB/s
Practical Commands
1. Writing an ISO Image to a USB Drive (Creating Bootable USB)
Commonly used to create Linux installation media.
Note: The device path for of= (e.g., /dev/sdX) varies by environment. A mistake here will erase your hard drive.
# Write the ISO file to the USB drive (/dev/sdX)
sudo dd if=./downloads/ubuntu-24.04-desktop-amd64.iso of=/dev/sdX bs=4M status=progress oflag=sync
Note: Adding status=progress displays a progress bar.
2. Creating a Disk Image, Formatting, and Mounting
This procedure creates a 1GB file, formats it as if it were a hard drive, and mounts it. This is often used for creating disks in virtualization environments (Xen, KVM).
# 1. Create a 1GB empty image file
dd if=/dev/zero of=virtual_disk.img bs=1M count=1024
# 2. Format with the ext4 file system
mkfs.ext4 virtual_disk.img
# 3. Create a mount point and perform a loopback mount
sudo mkdir -p /mnt/vdisk
sudo mount -o loop virtual_disk.img /mnt/vdisk
# Verify
df -h /mnt/vdisk
Execution Result Example (df command):
Filesystem Size Used Avail Use% Mounted on
/dev/loop0 976M 2.6M 907M 1% /mnt/vdisk
3. Converting Case in a Text File
dd can also be used as a filter for text data passed via pipes.
# Create a lowercase text file
echo "hello linux world" > lower.txt
# Convert to uppercase using conv=ucase
dd if=lower.txt of=upper.txt conv=ucase
# Verify result
cat upper.txt
Execution Result Example:
HELLO LINUX WORLD
Customization Points
- Adjusting Block Size (
bs=): The default is a small 512 bytes. For disk cloning, using larger values likebs=4Morbs=64Ksignificantly improves speed. - Progress Display (
status=progress): When handling massive files, always include this to eliminate the uncertainty of when the process will finish (available in GNU Coreutils 8.24+).
Important Notes
- Fatal Device Errors: Specifying a system disk like
of=/dev/sdawill result in irreversible data loss of the entire OS. Always verify device names usinglsblk. - Write Completion Timing: Even after the
ddcommand finishes, data may remain in the OS cache and not be fully written to the disk. Always run thesynccommand or properly unmount the USB drive before removing it. - Partitions vs. Full Disks: To write to an entire USB drive, use
/dev/sdX(no number). Specifying/dev/sdX1(a partition) may prevent the drive from booting.
Advanced Application
Triggering Progress Output from a Running dd Command (Signal Sending)
Older versions of dd do not support status=progress. In such cases, you can send a USR1 signal from another terminal to force the running dd to output its progress to standard error.
Procedure:
- Terminal A: Run the long-running process.Bash
dd if=/dev/zero of=huge_file.img bs=1M count=10000 - Terminal B: Find the process ID and send the signal.Bash
# Identify the PID and send kill -USR1 pkill -USR1 -x dd
Result Example (appears in Terminal A):
3560+0 records in
3560+0 records out
3732930560 bytes (3.7 GB) copied, 15.2341 s, 245 MB/s
Conclusion
The dd command is a powerful tool responsible for the physical movement and processing of data in Linux.
Warning: Mistakes in the of= (output) destination lead directly to data loss.In server environments without GUI tools or when extracting data from a failing HDD (conv=noerror), this command is often the only reliable solution.
Best Use Cases: Creating bootable USBs, full disk backups, and generating dummy test files.
Key Enhancements: Increase bs for speed; use status=progress for visibility.
