Linux dd Command: What It Is and How to Use It

Go beyond copying files and clone your entire drive

The Linux dd command doesn't officially stand for disk dump, but you can think of it that way. And despite the unassuming name it's a very useful Linux terminal program. From backups to writing USB drives, knowing how to use dd is definitely a good addition to your Linux toolbox. 

dd: The "Disk Dump" Command 

At its core, dd was made to copy and convert files from one format to another. Not formats like Word or PDF, but very low-level formats that describe how data is represented on disk. This was back in the mainframe days, when different systems had their own in-house formats, and solving interchange issues was a major undertaking.

The dd command takes bits from one file, and moves them to another file. What's important to understand is that dd simply moves bits from here to there, regardless of file system formats, folders, or extensions. This means you can accomplish a lot very quickly, but it also means you need to be careful when you run it.

With this in mind, the clever folks in the Linux community ported dd over for another purpose. Since in Linux "everything is a file," dd provides an easy way burn an ISO image to a flash drive. It does this by taking the image file and "copying" it to the file that represents the thumb drive. There are all sorts of scenarios where this is useful, but we'll focus on two here: writing removable media, and making a backup. 

Basic Usage of the dd Command

The basic usage of dd uses the following syntax: 

dd option1=value1 option2=value2

There's a whole slew of options available for dd, but here's the ones you should know to get started: 

  • bs: Block Size describes how much data will be read and written at one time. The trade-off with this option is between speed of completing the operation, and how easy it is to recover in the event of a failure. Larger block sizes complete more quickly, but are harder to recover.
  • if: The Input File is the source of the data.
  • of: The Output File is the destination for all the data being read in from the input source.
  • status: This is an optional flag, but a useful one that will provide you some information as your command executes. We'll give this a value of progress so it lets us know the command is still working.

Using dd to Write Removable Media

In this first example, we'll try one of the most common use cases for dd: writing removable media. You'd do this, for example, to "burn" an installation image for Linux onto a USB thumb drive.

Many install images come as an .ISO file, which is intended to be burned to a CD- or DVD-ROM drive, but you can burn the .ISO image to your USB drive with a command like the following:

dd bs=4M if=/path/to/imagefilename.img of=/dev/sda status=progress

In the above, we're taking the contents of imagefilename.img and copying it 4 MB at a time to /dev/sda. This is the file with the /dev file sub-system that represents our USB drive. We use the 4 MB block size here because most install images are big, and if we use a smaller one it will take a long time to finish. In any case something goes amiss it's not a big deal, just re-format your thumb drive and give it another try.

Make sure you have the correct values for the input and output. As mentioned, dd is designed to move bits from one location to another, and it doesn't care what it might overwrite in the process. Additionally, ensure there's nothing on the target drive that you need. At best, it will be very difficult to recover, and at worst it may be gone for good.

Using dd to Back Up a Linux PC 

You can also do the above process in reverse and write the contents of an entire drive to an image file. This is a very convenient and thorough way of backing up your hard drive.

Running the Linux dd Command

In this case, we can basically swap the "in file" for one of our drives, and the "out file" for an image:Drive

dd bs=64k if=/dev/nvme0n1 of=/home/aaron/nvmedrivebackup.img status=progress

Here we're selecting one of the partitions on our laptop's NVMe drive as the input, and providing a path and filename for the image as the output.

We're using a smaller byte size to avoid any issues with the copy, as this will be our recovery image in the event of a catastrophe. In order to restore the disk, simply reverse the input and output files.

The dd Command Is a Powerful Utility, So Use It Wisely

The dd command is a very powerful utility, and with some clever thinking you can probably uncover even more uses for it. However, it's also a very simple command, so make sure you've either plucked your options from someone knowledgeable, or you've triple-checked that you're not mixing up your input and output files. Otherwise you might end up writing all the empty bits from some blank media to the disk of your PC.