Eight Use Cases of the dd Command (with examples)

Eight Use Cases of the dd Command (with examples)

  • Osx
  • November 5, 2023

1. Make a bootable USB drive from an isohybrid file

Code:

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

Motivation: Making a bootable USB drive from an isohybrid file is useful when you want to install an operating system on a computer without an optical drive. The dd command allows you to copy the contents of the ISO file to the USB drive, making it bootable.

Explanation:

  • if=path/to/file.iso specifies the input file, in this case, the path to the isohybrid file.
  • of=/dev/usb_device specifies the output file, in this case, the path to the USB drive.
  • status=progress shows the progress of the copying process.

Example Output:

1048576000 bytes (1.0 GB, 1000 MiB) copied, 120 s, 8.7 MB/s

2. Clone a drive to another drive

Code:

dd if=/dev/source_device of=/dev/dest_device bs=4m conv=noerror status=progress

Motivation: Cloning a drive to another drive is useful when you want to create an exact replica of a drive, including its partitions and files. This can be helpful for creating backups or transferring data between drives.

Explanation:

  • if=/dev/source_device specifies the input file, in this case, the path to the source device.
  • of=/dev/dest_device specifies the output file, in this case, the path to the destination device.
  • bs=4m defines the block size to be used during the copying process. In this example, a block size of 4 MB is used.
  • conv=noerror specifies that the copying process should continue even if errors are encountered.
  • status=progress shows the progress of the copying process.

Example Output:

137438953472 bytes (137 GB, 128 GiB) copied, 5236 s, 26.2 MB/s

3. Generate a file of random bytes

Code:

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

Motivation: Generating a file of random bytes can be useful in various scenarios, such as generating cryptographic keys, testing file I/O performance, or creating random data for experimentation.

Explanation:

  • if=/dev/urandom specifies the input file, in this case, the kernel random driver which generates random bytes.
  • of=path/to/random_file specifies the output file, in this case, the path where the random file will be created.
  • bs=100 sets the block size to 100 bytes.
  • count=1 determines the number of blocks to be copied, in this case, 1 block of 100 bytes.

Example Output: The generated file will contain 100 bytes of random data. As the data is random, the specific output cannot be determined.

4. Benchmark the write performance of a disk

Code:

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

Motivation: Benchmarking the write performance of a disk allows you to assess the speed at which data can be written to the disk. This information is useful when comparing different storage devices or optimizing disk usage.

Explanation:

  • if=/dev/zero specifies the input file, in this case, the null device which provides an endless stream of zeros.
  • of=path/to/1GB_file specifies the output file, in this case, the path where the file will be created.
  • bs=1024 sets the block size to 1024 bytes.
  • count=1000000 determines the number of blocks to be copied, in this case, 1000000 blocks of 1024 bytes each, resulting in a 1GB file.

Example Output:

1048576000 bytes (1.0 GB, 1000 MiB) copied, 3.77472 s, 277 MB/s

5. Generate a system backup into an IMG file

Code:

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

Motivation: Creating a system backup into an IMG file is useful when you want to create a snapshot of a specific drive or partition. This backup can then be used to restore the drive to its original state if necessary.

Explanation:

  • if=/dev/drive_device specifies the input file, in this case, the path to the drive or partition to be backed up.
  • of=path/to/file.img specifies the output file, in this case, the path where the IMG file will be created.
  • status=progress shows the progress of the copying process.

Example Output:

536870912 bytes (537 MB, 512 MiB) copied, 10 s, 53.7 MB/s

6. Restore a drive from an IMG file

Code:

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

Motivation: Restoring a drive from an IMG file allows you to recover data or restore a drive to a previous state. This can be useful when recovering from a system failure, migrating to a different drive, or reverting changes made to a drive.

Explanation:

  • if=path/to/file.img specifies the input file, in this case, the path to the IMG file containing the backup.
  • of=/dev/drive_device specifies the output file, in this case, the path to the drive where the IMG file will be restored.
  • status=progress shows the progress of the copying process.

Example Output:

1073741824 bytes (1.1 GB, 1.0 GiB) copied, 15 s, 71.6 MB/s

7. Check the progress of an ongoing dd operation

Code:

kill -USR1 $(pgrep ^dd)

Motivation: Checking the progress of an ongoing dd operation can be useful to estimate the time remaining or to monitor the status of a lengthy copying process. By sending a specific signal to the dd process, it will output the current progress without interrupting the operation.

Explanation:

  • kill -USR1 sends the USR1 signal to the dd process.
  • $(pgrep ^dd) retrieves the process ID (PID) of the running dd process.

Example Output: The output will depend on the specific dd process and its progress. An example output could be:

1287351+0 records in
1287351+0 records out
660837888 bytes (661 MB, 630 MiB) copied, 100 s, 6.6 MB/s

Conclusion

In this article, we have explored eight different use cases of the dd command. From creating bootable USB drives to generating random files, benchmarking disk performance, and performing drive cloning and restoration, dd is a versatile tool for various data manipulation tasks. By understanding these use cases and their corresponding code examples, you can leverage the power of dd to efficiently manage and manipulate data in your system.

Tags :

Related Posts

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

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

The ’ls’ command is used to list the contents of a directory.

Read More
Git Mergetool (with examples)

Git Mergetool (with examples)

Code Example 1: Launch the default merge tool to resolve conflicts git mergetool Motivation: The git mergetool command is used to launch the default merge tool provided by Git in order to resolve merge conflicts.

Read More
How to use the command cpufreq-aperf (with examples)

How to use the command cpufreq-aperf (with examples)

The cpufreq-aperf command is used to calculate the average CPU frequency over a specified time period.

Read More