dd Command ExamplesThe 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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.