Mastering dd Command: Comprehensive Guide with Practical Examples

What you’re reading is only the first of many articles from the “Learning Linux Commands” series. Why would we want to do such a thing? Because it’s useful to have every option and possible use of a widely used command all in one place. You will find some options or even some commands that you didn’t even know existed, and your life as a Linux user/admin will become easier. If you’re not afraid of opening a terminal and know the basics of using a Linux system, this article is for you.

In this tutorial you will learn:

  • General usage of the dd command
  • Examples of dd command usage in various scenarios
Mastering dd Command: Comprehensive Guide with Practical Examples
Mastering dd Command: Comprehensive Guide with Practical Examples
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Linux-based operating system
Software dd utility
Other Basic knowledge of terminal commands
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Learning Linux dd Command with Examples

The dd command is a powerful utility for Unix and Unix-like systems, used to convert and copy files. In this article, we will explore various examples of how to use the dd command effectively.

  1. Filling a drive with random data: This command fills the drive with random data. Be careful as it will overwrite existing data on the drive.
    dd if=/dev/urandom of=/dev/sda bs=4k

    This command is useful for securely wiping a drive by overwriting it with random data.

  2. Drive-to-drive duplication: This command duplicates the contents of one drive to another.
    dd if=/dev/sda of=/dev/sdb bs=4096

    Useful for creating a backup of a drive or migrating data to a new drive.

  3. Cleaning up a hard drive: This command writes zeros to a drive, effectively erasing its contents.
    dd if=/dev/zero of=/dev/sda bs=4k

    It may need to be repeated to ensure complete erasure.

  4. Copying from file to tape device: This command copies data from a file to a tape device.
    dd if=inputfile of=/dev/st0 bs=32k conv=sync

    The conv=sync option ensures synchronized I/O operations.

  5. Copying data from tape to file: This command reverses the previous operation, copying data from a tape device to a file.
    dd if=/dev/st0 of=outfile bs=32k conv=sync
  6. Checking if a drive is zeroed out: This command reads a drive and checks for non-zero data.
    dd if=/dev/sda | hexdump -C | grep [^00]

    This helps verify if a drive has been successfully wiped.

  7. Filling out a partition: This command fills a partition with random data.
    dd if=/dev/urandom of=/home/$user/hugefile bs=4096

    Be cautious with system partitions as it will overwrite data.

  8. Scrambling a file: This command overwrites a file with random data before deleting it.
    ls -l myfile
    -rw-r--r-- 6703104 Oct 31 18:25 myfile
    dd if=/dev/urandom of=myfile bs=6703104 count=1
    rm myfile

    This can be used to securely delete files.

  9. Copying a partition to another partition: This command copies data from one partition to another without truncating.
    dd if=/dev/sda3 of=/dev/sdb3 bs=4096 conv=notrunc,noerror

    The conv=notrunc,noerror options ensure no truncation and continue on read errors.

  10. Creating a gzipped image of a partition: This command creates a compressed image of a partition.
    dd if=/dev/sdb2 ibs=4096 | gzip > partition.image.gz conv=noerror

    This is useful for creating backups of partitions.

  11. Copying tape drive contents to a file: This command copies the contents of a tape drive to a file, converting from EBCDIC to ASCII.
    dd bs=10240 cbs=80 conv=ascii,unblock if=/dev/st0 of=ascii.out
  12. Copying from 1KB block device to 2KB block device: This command copies data between devices with different block sizes.
    dd if=/dev/st0 ibs=1024 obs=2048 of=/dev/st1
  13. Copying zeros to /dev/null: This command is useful for benchmarking I/O speed by copying zeros to the null device.
    dd if=/dev/zero of=/dev/null bs=100M count=100
    100+0 records in
    100+0 records out
    10485760000 bytes (10 GB) copied, 5.62955 s, 1.9 GB/s
  14. Erasing GPT from disk: This command erases the GPT from a disk by writing zeros to the beginning and end of the drive.
    dd if=/dev/zero of=/dev/sda bs=512 count=2
    fdisk -s /dev/sda
    dd if=/dev/zero of=/dev/sda seek=(number_of_sectors - 20) bs=1k



  15. Creating a bootable USB drive: This command writes an image file to a USB drive to make it bootable.
    dd if=/location/of/bootimage.img of=/dev/sdX
  16. Checking for bad blocks: This command reads a drive to check for bad blocks.
    dd if=/dev/sda of=/dev/null bs=1m
  17. Copying the MBR to a floppy: This command copies the Master Boot Record (MBR) to a floppy disk.
    dd if=/dev/sda of=/dev/fd0 bs=512 count=1

    This is useful for creating backups of the MBR.

  18. Drive-to-drive duplication with partitions: This command duplicates a specific partition from one drive to another.
    dd if=/dev/sda1 of=/dev/sdb1 bs=4096
  19. Creating a CD image: This command creates an ISO image of a CD.
    dd if=/dev/sr0 of=/home/$user/mycdimage.iso bs=2048 conv=nosync
  20. Replacing a disk with another of identical size: This command is useful when replacing a disk with another of identical size.
    dd if=/dev/sda of=/dev/sdb bs=64k conv=sync
  21. Creating DVD images of a partition: This command creates multiple DVD images of a partition for backup purposes.
    dd if=/dev/sda2 of=/home/$user/hddimage1.img bs=1M count=4430
    dd if=/dev/sda2 of=/home/$user/hddimage2.img bs=1M count=8860
    [...]
  22. Restoring from a backup: This command restores data from the previously created DVD images.
    dd if=/$location/hddimage1.img of=/dev/sda2 bs=1M
    dd if=/$location/hddimage2.img of=/dev/sda2 seek=4430 bs=1M
    dd if=/$location/hddimage3.img of=/dev/sda2 seek=8860 bs=1M
    [and so on...]
  23. Destroying the superblock: This command destroys the superblock of a filesystem.
    dd if=/dev/zero count=1 bs=1024 seek=1 of=/dev/sda6
  24. Another way to destroy the superblock: This command provides another method to destroy the superblock.
    dd if=/dev/zero count=1 bs=4096 seek=0 of=/dev/sda5
  25. Checking a file for viruses: This command checks a file for viruses using ClamAV.
    dd if=/home/$user/suspicious.doc | clamscan -
  26. Looking at the contents of a binary file: This command views the contents of a binary file using hexdump.
    dd if=/home/$user/binary file | hexdump -C | less
  27. Benchmarking hard drive read/write speed: This command benchmarks the read/write speed of a hard drive.
    dd if=/home/$user/bigfile of=/dev/null
    dd if=/dev/zero of=/home/$user/bigfile bs=1024 count=1000000
  28. Reviving older hard drives: This command helps revive older hard drives by writing and reading data.
    dd if=/dev/sda of=/dev/sda
  29. Examining memory contents: This command examines memory contents for human-readable strings.
    dd if=/dev/mem | strings | grep 'string_to_search'



  30. Copying a floppy disk: This command creates an image of a floppy disk.
    dd if=/dev/fd0 of=/home/$user/floppy.image bs=2x80x18b conv=notrunc
  31. Creating a 1KB file of random gibberish: This command creates a small file with random data.
    dd if=/dev/urandom of=/home/$user/myrandom bs=100 count=1
  32. Creating an image of system memory: This command creates an image of the current state of system memory.
    dd if=/dev/mem of=/home/$user/mem.bin bs=1024
  33. Printing a file to stdout: This command prints the contents of a file to the terminal.
    dd if=/home/$user/myfile
  34. Searching a partition for a string: This command searches for a specific string within a partition.
    dd if=/dev/sda2 bs=16065 | hexdump -C | grep 'text_to_search'
  35. Skipping the first 64KB of a file: This command copies a file, skipping the first 64KB.
    dd if=/home/$user/file.bin skip=64k bs=1 of=/home/$user/convfile.bin
  36. Creating a bootable USB drive: This command writes an image file to a USB drive to make it bootable.
    dd if=/home/$user/bootimage.img of=/dev/sdc
  37. Reading BIOS: This command reads the BIOS contents.
    dd if=/dev/mem bs=1k skip=768 count=256 2>/dev/null | strings -n 8
  38. Converting Nero image to ISO: This command converts a Nero image to a standard ISO image.
    dd bs=1k if=imagefile.nrg of=imagefile.iso skip=300k
  39. Creating a temporary swap space: This command creates a temporary swap file.
    dd if=/dev/zero of=tmpswap bs=1k count=1000000
    chmod 600 tmpswap
    mkswap tmpswap
    swapon tmpswap
  40. Determining I/O speed: This command measures the I/O speed of a drive by reading 1GB of data.
    dd if=/dev/sda of=/dev/null bs=1024k count=1024
    1073741824 bytes (1.1 GB) copied, 24.1684 s, 44.4 MB/s
  41. Generating a random number: This command generates a random number.
    dd if=/dev/random count=1 2>/dev/null | od -t u1 | awk '{ print $2}' | head -1
  42. Copying RAM to a file: This command creates an image of the system RAM.
    dd if=/dev/mem of=myRAM bs=1024
  43. Viewing MBR content: This command displays the content of the Master Boot Record in hex and ASCII.
    dd if=/dev/sda bs=512 count=1 | od -xa
  44. Restoring MBR without partition table: This command restores the MBR without disturbing the partition table.
    dd if=/my/old/mbr of=/dev/sda bs=446 count=1
  45. Creating partition copy with limited size: This command creates a copy of a partition, splitting it into smaller files.
    dd if=/dev/sda1 | split -b 700m - sda1-image
  46. Converting text to uppercase: This command converts the output of a command to uppercase.
    ls -l | dd conv=ucase



  47. Converting text to lowercase: This command converts any text to lowercase.
    echo "MY UPPER CASE TEXT" | dd conv=lcase
  48. Converting system password file to EBCDIC: This command converts the system password file to fixed-length EBCDIC format.
    dd if=/etc/passwd cbs=132 conv=ebcdic of=/tmp/passwd.ebcdic
  49. Converting ASCII to EBCDIC: This command converts a text file from ASCII to EBCDIC.
    dd if=text.ascii of=text.ebcdic conv=ebcdic
  50. Converting a file to uppercase: This command converts the contents of a file to uppercase.
    dd if=myfile of=myfile conv=ucase

Conclusion

This has been just a small part of what dd can do, and we hope that this article managed to comprise the most useful examples for the everyday user. However, before you go further, we recommend you read your hard drive’s documentation, looking for things like LBA limitation, and take extra care when using dd in a root terminal. Of course, you already have backups, but a little extra care will save you hours of unnecessary work.



Comments and Discussions
Linux Forum