Linux dd Command Examples

The dd command in Linux is a powerful utility for copying and converting data. It is often used for low-level operations on files, disks, or partitions. Below are 10 examples demonstrating various uses of the dd command with detailed explanations.

Warning: The dd command can be destructive if used incorrectly. Always double-check your input and output parameters and ensure you have backups before proceeding.

1. Create a Backup of a Disk Partition

Creates a backup of a disk partition to an image file.

sudo dd if=/dev/sda1 of=/backup/partition_backup.img

Explanation: if=/dev/sda1 specifies the input partition, and of=/backup/partition_backup.img specifies the output file. Requires superuser privileges.

2. Restore a Disk Partition from a Backup

Restores a partition from a previously created image file.

sudo dd if=/backup/partition_backup.img of=/dev/sda1

Explanation: Reverses the input and output to write the image back to the partition. Ensure the partition is unmounted.

3. Copy a File with Block Size Specification

Copies a file while specifying the block size for faster transfer.

dd if=inputfile.txt of=outputfile.txt bs=64K

Explanation: bs=64K sets the block size to 64 kilobytes, which can improve performance compared to the default smaller block size.

4. Create a File of Specific Size with Zeroes

Generates a file filled with zeroes, useful for testing or reserving space.

dd if=/dev/zero of=testfile.bin bs=1M count=100

Explanation: if=/dev/zero provides a stream of zeroes, bs=1M sets block size to 1 megabyte, and count=100 creates a 100 MB file.

5. Create a File with Random Data

Generates a file filled with random data, useful for testing or secure wiping.

dd if=/dev/urandom of=randomfile.bin bs=1M count=10

Explanation: if=/dev/urandom provides random data, creating a 10 MB file with bs=1M and count=10.

6. Clone an Entire Disk

Copies an entire disk to another disk.

sudo dd if=/dev/sda of=/dev/sdb bs=64K

Explanation: Clones /dev/sda to /dev/sdb. Ensure /dev/sdb is at least as large as /dev/sda. Use with caution.

7. Create an ISO Image from a CD/DVD

Extracts the contents of a CD/DVD to an ISO file.

sudo dd if=/dev/cdrom of=/backup/disc_image.iso

Explanation: if=/dev/cdrom reads from the CD/DVD drive, and of=/backup/disc_image.iso saves it as an ISO file.

8. Wipe a Disk with Zeroes

Overwrites a disk with zeroes to securely erase data.

sudo dd if=/dev/zero of=/dev/sdb bs=4M status=progress

Explanation: if=/dev/zero writes zeroes to /dev/sdb. status=progress shows transfer progress. Be extremely careful, as this erases all data.

9. Convert File to Uppercase

Converts a text file to uppercase using conv=ucase.

dd if=input.txt of=output.txt conv=ucase

Explanation: Reads input.txt, converts all lowercase characters to uppercase, and writes to output.txt.

10. Benchmark Disk Write Speed

Tests disk write performance by writing a large file.

dd if=/dev/zero of=testfile.bin bs=1M count=1000 oflag=dsync

Explanation: Writes a 1 GB file (bs=1M, count=1000) with oflag=dsync to ensure each write is synced, providing accurate benchmark results. The command outputs the transfer speed.

Note: Always verify the input (if) and output (of) parameters to avoid data loss. Use man dd for more options and details. Examples assume you have appropriate permissions and devices available.