How to use the command 'dd' (with examples)

How to use the command 'dd' (with examples)

The ‘dd’ command is a versatile utility in Unix-like operating systems that is used for converting and copying files. It can be particularly useful for creating bootable USB drives, cloning drives, generating random files, benchmarking disk performance, and creating and restoring system backups.

Use case 1: Make a bootable USB drive from an isohybrid file

dd if=path/to/file.iso of=/dev/usb_drive status=progress

Motivation: This use case is helpful when you want to create a bootable USB drive from an isohybrid file, such as an Arch Linux ISO. It allows you to quickly transfer the ISO image to the USB drive without any intermediate steps.

Explanation:

  • if=path/to/file.iso: Specifies the input file (source) which is the ISO image you want to copy to the USB drive.
  • of=/dev/usb_drive: Specifies the output file (destination) which is the USB drive you want to make bootable.
  • status=progress: Displays the progress of the operation while copying the ISO image to the USB drive.

Example output: The command will display the progress of the copying operation, showing the amount of data transferred and the elapsed time.

Use case 2: Clone a drive to another drive with 4 MiB block

dd if=/dev/source_drive of=/dev/dest_drive bs=4M conv=noerror status=progress

Motivation: Cloning a drive is useful for creating backups or transferring data to a new drive. The ‘dd’ command with a larger block size (4 MiB) helps to improve the performance of the cloning process.

Explanation:

  • if=/dev/source_drive: Specifies the input file (source) which is the drive you want to clone.
  • of=/dev/dest_drive: Specifies the output file (destination) which is the drive you want to clone to.
  • bs=4M: Sets the block size to 4 MiB, improving the performance of the cloning process.
  • conv=noerror: Instructs ‘dd’ to continue copying despite read errors on the source drive.
  • status=progress: Displays the progress of the cloning operation.

Example output: The command will display the progress of the cloning operation, showing the amount of data transferred and the elapsed time.

Use case 3: Generate a file of 100 random bytes using kernel random driver

dd if=/dev/urandom of=path/to/random_file bs=100 count=1

Motivation: Sometimes you may need to create a file with random content, such as when generating encryption keys or test data. The ‘dd’ command with ‘/dev/urandom’ as the input file provides a simple and efficient method to achieve this.

Explanation:

  • if=/dev/urandom: Specifies the input file (source) as the kernel random driver, which generates random data.
  • of=path/to/random_file: Specifies the output file (destination) where the generated random data will be saved.
  • bs=100: Sets the block size to 100 bytes, ensuring that the output file contains exactly 100 random bytes.
  • count=1: Limits the number of blocks to generate to 1, so only a single block of random data is produced.

Example output: After running the command, the output file located at ‘path/to/random_file’ will contain 100 random bytes.

Use case 4: Benchmark the write performance of a disk

dd if=/dev/zero of=path/to/file_1GB bs=1024 count=1000000

Motivation: Benchmarking the write performance of a disk can help you assess the speed and efficiency of a storage device. The ‘dd’ command, combined with ‘/dev/zero’ as the input file, provides a convenient way to measure the write speed of a disk.

Explanation:

  • if=/dev/zero: Specifies the input file (source) as ‘/dev/zero’, which produces an endless stream of null bytes.
  • of=path/to/file_1GB: Specifies the output file (destination) where the null bytes will be written to benchmark the disk’s write speed.
  • bs=1024: Sets the block size to 1024 bytes, determining the size of each write operation during benchmarking.
  • count=1000000: Sets the number of blocks to write, which in this case is 1000000 blocks of size 1024 bytes, resulting in a 1GB file.

Example output: The command will create a file named ‘file_1GB’ located at ‘path/to/file_1GB’ with a size of 1GB, consisting entirely of null bytes.

Use case 5: Generate a system backup into an IMG file

dd if=/dev/drive_device of=path/to/file.img status=progress

Motivation: Creating system backups is essential to safeguard your data and restore your system in case of a failure. The ‘dd’ command can be used to generate a complete image of a drive, allowing you to create a full system backup in an IMG file.

Explanation:

  • if=/dev/drive_device: Specifies the input file (source) as the drive device you want to back up.
  • of=path/to/file.img: Specifies the output file (destination) where the drive image will be saved.
  • status=progress: Displays the progress of the backup operation.

Example output: The command will display the progress of the backup operation, showing the amount of data transferred and the elapsed time.

Use case 6: Restore a drive from an IMG file

dd if=path/to/file.img of=/dev/drive_device status=progress

Motivation: Restoring a drive from an IMG file is useful when you want to recover a system backup or clone a previously backed-up drive. The ‘dd’ command allows you to restore the drive image to the original drive or a different drive.

Explanation:

  • if=path/to/file.img: Specifies the input file (source) as the IMG file containing the drive image you want to restore.
  • of=/dev/drive_device: Specifies the output file (destination) as the drive device you want to restore the image to.
  • status=progress: Displays the progress of the restore operation.

Example output: The command will display the progress of the restore operation, showing the amount of data transferred and the elapsed time.

Use case 7: Check the progress of an ongoing dd operation

kill -USR1 $(pgrep ^dd)

Motivation: Occasionally, you may want to check the progress of an ongoing ‘dd’ operation to monitor its status. This becomes especially helpful when the progress is not continuously displayed.

Explanation:

  • kill -USR1 $(pgrep ^dd): Sends the USR1 signal to the ‘dd’ process. This signal causes ‘dd’ to output the progress information without terminating the operation.
    • pgrep ^dd: Retrieves the process ID of the ‘dd’ command running in the system.

Example output: After executing the command, the ‘dd’ process will output the progress information, including the amount of data transferred, the transfer speed, and the elapsed time.

Tags :

Related Posts

Using the `for` Command (with examples)

Using the `for` Command (with examples)

The for command is a useful looping construct in Bash that allows you to execute a command or a set of commands multiple times.

Read More
Using systemd-delta (with examples)

Using systemd-delta (with examples)

Introduction The systemd-delta command is a useful tool that allows you to find overridden systemd-related configuration files in your system.

Read More
How to use the command 'qm guest exec-status' (with examples)

How to use the command 'qm guest exec-status' (with examples)

The command ‘qm guest exec-status’ is used to print the status of a specific PID started by the guest-agent on QEMU/KVM Virtual Machine Manager.

Read More