Learn Linux dd Command with 17 Examples

Linux TLDR
Last Updated:
Reading time: 8 minutes

The dd command is a command-line utility that is abbreviated as “Data Definition“, “Data Duplicator“, or “Disk Dump” depending upon the usage, but it’s commonly known as a utility for copying and converting data in Linux.

It can copy data from a file or block device (like a hard drive or USB flash drive) to another and perform various operations like creating backups, cloning hard drives, making bootable USB flash drives, data compression, and many more.

Knowing all this might make you more attracted to this command, but before that, you should know that this command is able to overwrite or destroy data from the disk if used improperly. It is recommended that the user thoroughly understand the options and arguments of the command before using it.

In this article, you’ll learn how to use the dd command and its options, as well as some common ways to use it as you learn more about Linux.

Tutorial Details

DescriptionConvert and Copy a File
Difficulty LevelHigh
Root or Sudo PrivilegesYes
Host System and ArchitectureUbuntu 22.10 (x64)
OS Compatibility Ubuntu, Manjaro, Fedora, etc.
Prerequisitesdd
Internet RequiredNo
Discussed Tools in this Article

Syntax of the dd Command

The dd command accepts three arguments: one is the option (optional), and the other two are standard input and output, which are specified by the “if” (for input file) and “of” (for output file) options, respectively.

$ dd if=<input-file-name> of=<output-file-name> [OPTION]

Note: You need to be very careful while specifying the “if” and “of” options; a single wrong option or argument can result in data loss. If you are totally unsure, then reach out to us in the comment section for help.

This command can be used in a lot of different ways, and this article will talk about the most important ones.

Linux dd Command Examples

This article will mostly focus on the practical usage of this command, and the required option for it will be explained along the way. So, letโ€™s start with

1. Backing Up an Entire Disk or Partition

In the syntax section, you learned about the “if” (for input file) and “of” (for output file) options that can be used to duplicate data from one source to another.

Donโ€™t confuse this command with the traditional cp command for copying files from one source to another; here we are discussing copying at the block level.

In respect to that, it’s recommended not to use this command for simple copy tasks, as the cp command does it in a much simpler way.

a. Cloning Entire Disks/Partitions

Let’s consider you have two “sda” and “sdb” disks and want to clone the data from one disk to another at the partition level.

In this case, you have to make sure that the destination “sdb” disk has enough disk space to store source “sda” data; if it does not, then you have to resize the entire disk to match the exact size of the “sda” disk.

Once you’re ready to clone a disk, you can invoke the fdisk command with the “-l” flag.

$ sudo fdisk -l

The above command will list available disks and partitions as well as their sizes. So, assuming that “/dev/sda” is the source and “/dev/sdb” is the destination with the same disk size, then invoke the following command to start the cloning.

Note: You can use the “status=progress” option to show the progress of data transfer.

$ sudo dd if=/dev/sda of=/dev/sdb status=progress

b. Creating a Disk Image of Disks/Partitions

In contrast to the previous example, if you do not want to copy your entire “sda” disk data to a “sdb” disk and instead want to create a disk image of “sda” to an “ABC.img” file, then you can do that as well.

Just make sure that the target location has the required amount of space to save the โ€œABC.imgโ€ file. If you are sure about that, then invoke the following command to start the process.

$ dd if=/dev/sda of=sda_backup.img

c. Creating a Compressed Image of Disks/Partitions

A disk image can take up a lot of disk space; however, you can use the gzip tool to create a compressed image of your disks/partitions that will drastically reduce the size of the output file.

The following command will create a compressed disk image of the โ€œ/dev/sdaโ€ disk.

$ sudo dd if=/dev/sda | gzip -c > sda_backup.tar.gz

In the preceding command, a pipe is used to redirect the dd stdout (or standard output) as an gzip stdin (or standard input). For more information, check out the redirection symbol article.

d. Restoring a Disk or a Partition Image

The restoration process is pretty simple; you just need to inverse the input file. However, make sure that the destination has enough space and does not contain any files to avoid overwriting unless you intentionally want to.

Execute the following command to restore the “sda_backup.img” file to the “sda” disk:

$ sudo dd if=sda_backup.img of=/dev/sda

Once the restoration process is done, the disk state will be changed back to its previous state at the time of the backup.

e. Restoring a Disk or a Partition Compressed Image

In this case, pass the compressed file as an argument to the gzip command with the “-dc” flag to uncompress the file and move the content to a “sda” disk using the pipe.

$ sudo gzip -dc sda_backup.tar.gz | dd of=/dev/sda

Now, letโ€™s see how you can create a virtual filesystem.

2. Creating a Virtual Filesystem or Adding Null Data to a File

A virtual filesystem is a filesystem that exists in a file format rather than on a physical storage device. These types of filesystems are used for special purposes like creating temporary storage, storing process information, or caching.

a. Creating a Virtual Filesystem

For the explanation, we will create a “zero_file” using the “/dev/zero” filesystem that is used to provide an infinite stream of zero-valued bytes. It is often used to create a file filled with null or zero bytes for the purpose of creating a file with a specific size.

$ dd if=/dev/zero of=zero_file bs=1M count=100

The above command will create a “zero_file” with 100 MB of zero-valued bytes from “/dev/zero“.

Whereas,

  • The “bs” option defines the block size.
  • The “count” option defines the number of input blocks to be copied.

Calculation: bs (1) x count (100) = zero_file (100MB).

b. Modifying the First 100 Bytes of a File with Null Data

Unlike the previous command, you can also modify the first 100 bytes of a file with null data.

$ dd if=/dev/null of=file.txt bs=1 count=100 conv=notrunc

Whereas,

  • The null data will be taken from “/dev/null“.
  • Overwrite the first 100 bytes of the referenced “file.txt” file.
  • Use a block size of 1 byte.
  • Use a count of 100 bytes.
  • Use the “conv=notrunc” option to ensure that the rest of the file is not truncated.

While using the above command, be careful; otherwise, you will end up overwriting the important data.

3. Creating/Mounting a Backup Image of a CD/DVD & Creating a Bootable USB Flash Drive

As CDs and DVDs become less common in computing, you can make a backup image of your referenced CD or DVD and then use that image to make a bootable flash drive.

a. Creating a Backup Image of a CD/DVD

Execute the following command to create a backup of your CD/DVD using the dd command:

$ dd if=/dev/cdrom of=cd_backup.iso bs=2048

Whereas,

  • The CD/DVD drive is represented by the device file “/dev/cdroom“.
  • Writing the data to a file named “cd_backup.iso” in your existing directory.
  • Use a block size of 2048 bytes.

b. Mounting a Backup Image of a CD/DVD

To mount the previously created ISO image, you can specify the “-o loop” option to associate the file with a loop device, which makes the file accessible as a block device.

Also Read: Mount and Unmount File Systems in Linux

$ sudo mount -o loop cd_backup.iso /mnt

After the image is mounted, you can access the files inside the image by navigating to the “mnt” directory. When you’re done, you can use the “umount” command to unmount the image.

$ sudo umount mnt

c. Creating a Bootable USB Flash Drive

The following command will create a bootable USB flash drive using the previously created image. Ensure to unmount the image first.

Note: A USB flash drive should have the space required by the ISO image.

$ sudo dd if=cd_backup.iso of=/dev/sdb bs=4M

Whereas,

  • The “cd_backup.iso” will be used as the source ISO file.
  • The data will be written directly to the USB flash drive, represented by the device file “/dev/sdb“.
  • Use a block size of 4 MB.

Be careful while selecting the correct device file; otherwise, the risk of data loss will increase. However, you can use the lsblk command to see the available storage devices and their device files.

4. Backup and Restore the MBR

The MBR (short for Master Boot Record) is used to store important pieces of data at the beginning of a hard drive or other storage device that is used to boot the system.

Backing up the MBR can be important in several cases, like system recovery, dual-booting, hard drive replacement, disk cloning, etc.

a. Backing Up the MBR to an Image File

The following command will backup the MBR of the first hard drive, usually represented by the device file “/dev/sda“:

$ sudo dd if=/dev/sda of=mbr.bin bs=512 count=1

Whereas,

  • Read the first 512 bytes of data from the device file “/dev/sda“, which represents the MBR.
  • Backup the content of MBR in the “mbr.bin” image.
  • Use a block size of 512 bytes and a count of 1 block.

b. Reading the MBR Image File

Using the hexdump command, you can read the previously backed up “mbr.bin” image in a human-readable format.

$ hexdump -C mbr.bin

Alternatively, you can use the od command to read the content of the “mbr.bin” image in hexadecimal format.

$ od -t x1 mbr.bin

c. Restoring MBR from MBR Image File

The following command will restore the MBR from a file named “mbr.bin” to the first hard drive, usually represented by the device file “/dev/sda“:

$ sudo dd if=mbr.bin of=/dev/sda bs=512 count=1

Whereas,

  • Read the data from “mbr.bin“, which represents the backup MBR image.
  • Write the data directly to the first hard drive, represented by the device file “/dev/sda“.
  • Use a block size of 512 bytes and a count of 1 block.

5. Converting the Data Formats of a File

If you have a file with data in EBCDIC format and your system supports ASCII format, you can change the data format of the file to the one that your system supports by using the dd command.

a. Converting the Data Format of a File from EBCDIC to ASCII

The following command will convert the data format for the referenced file to ASCII.

$ dd if=file.ebc of=file.asc conv=ascii

Whereas,

  • Read the data from the “file.ebc” file.
  • Write the data to the output file “file.asc“.
  • Use the “conv=ascii” option to specify the conversion format.

b. Converting the Data Format of a File from ASCII to EBCDIC

Execute the following command to convert the data format of a file from ASCII to EBCDIC.

$ dd if=file.asc of=file.ebc conv=ebcdic

The “conv=ebcdic” option will specify that the input data should be converted to EBCDIC.

6. Converting Case of a File

If you have a file with a specific or mixed case and want to generate a file with a specific case, like upper or lower, then you can follow the following methods:

a. Converting a File to Uppercase

The following command will generate a new file with all the content in uppercase.

$ sudo dd if=file.txt of=file2.txt conv=ucase

Whereas,

  • Read the data from the “file.txt” file.
  • Write the data to the output file “file2.txt“.
  • Use the “conv=ucase” option to specify the conversion format.

b. Converting a File to Lowercase

The following will change the case of the referenced file to lowercase.

$ sudo dd if=file2.txt of=file3.txt conv=lcase

The “conv=lcase” option will specify that the input data should be converted to lowercase.

So, here comes the end of the final example.

Wrap Up

I hope this quick but detailed article will help you understand the dd command more deeply. However, if you have any examples that should be included in this article or any questions or queries related to this topic, then feel free to ask them in the comment section.

Till then, peace!