How to Utilise dd Command in Linux – Guide

dd is a very powerful and useful utility available on Unix and Unix-like operating systems. As stated in its manual, its purpose is to convert and copy files. On Unix and Unix-like operating systems like Linux, almost everything is treated as a file, even block devices: it uses dd to clone a disk or wipe data, among other things. makes it useful. The dd utility is immediately available in the most minimal installation of all distributions. In that tutorial We will see how to use it and how we can modify its behavior using some of the most commonly used options to make your Linux system administration task easier.

Making a bootable USB drive with dd:

Creating a bootable USB drive of your favorite OS with the dd command is very easy. All you need is a USB drive and an ISO or IMG image of the operating system you want to create a bootable USB from. Let’s assume you have downloaded an Alpine Linux ISO image and the file is saved in the ~/Downloads directory as alpine-standard-3.8.0-x86_64.iso . Now, you can list all connected storage or block devices with the following command: As you can see, all connected storage or block devices are listed. Here /dev/sdb is my USB drive. It has two partitions, /dev/sdb1 and /dev/sdb2. But when you create bootable USB drives, you need to use /dev/sdb, the entire block device, not any partitions. Now create a bootable Alpine Linux USB drive with the following command: Here the if=~/Downloads/alpine-standard-3.8.0-x86_64.iso option is used to tell dd that the input file is in the path ~/Downloads/alpine-standard-3.8.0-x86_64.iso The =/dev/sdb option is used to tell dd that the output file is in the /dev/sdb path. bs=1M tells dd to read ~/Downloads/alpine-standard-3.8.0-x86_64.iso and write to /dev/sdb 1 Megabytes of data at a time. As you can see, the ISO file is copied to the /dev/sdb block device. Now you can use it to install Alpine Linux. This command is very destructive. The dd command clears the partition table and other metadata, block device flags. So you must be careful.

Displaying the progress bar:

By default, the dd command does not show any progress bars. But you can tell dd to show it with the status=progress option. For example, to copy data from /dev/sda to /dev/sdb 1 Megabytes at a time and also show the progress bar, run the following command: As you can see, the progress bar is displayed. You can see how much of the data is copied and the rate at which it is being copied.

Measuring read and write performance of a storage device with dd:

You can measure the read and write speed of a storage device with dd very easily. Of course, there are many graphics software that provide this information, but command line lovers would find this very interesting. First, you need to mount the partition or storage device on your file system. If you don’t have a partition on your storage device, you can always create it with the fdisk command and format it to your desired file system (like FAT32, EXT4, NTFS, XFS etc). Here I assume you have a /dev/sdb1 partition and it is formatted as EXT4. Let’s say you want to mount the /dev/sdb1 partition in the /mnt directory and run the following command: As you can see, the /dev/sdb1 partition is mounted in the /mnt directory. Now let’s create a 1GB testrw file in the /mnt directory with dd: Here, count=1 means, read bs=1G which is 1 Gigabyte of /dev/zero, and write to /mnt/testrw. The oflag=direct option is used to disable disk caching. If disk caching is enabled, you will not get very accurate results. As you can see, the write speed of my USB drive is around 6.1MB per second. You can also test the read speed of your storage device with the following command: As you can see, I can read at 4.3MB per second.

Testing storage device latency with dd:

The latency of a storage device is the time it takes to access the device. It is an important parameter that we can determine with the help of the dd command. To test for latency, we can write or read small pieces of data (about 512 bytes at a time) X times and see how long it takes. Then we can calculate how long it takes to read or write a single block of data very easily. This is called storage device latency. For example, let’s say you want to calculate write latency. Now run the following command to write a chunk of 512 bytes about 1000 times: As you can see, it takes about 16.4541 seconds to write 1000 blocks of 512-byte data. Now to write a single block of data it takes about (16.4541s / 1000 = 0.0164s) 0.0164 seconds. Therefore, the write latency is about 0.0164 seconds for this storage device.

Final note

I hope you like the guide How to Utilise dd Command in Linux. In case if you have any query regards this article you may ask us. Also, please share your love by sharing this article with your friends.