How to Use the dd Command in Linux

Learn how to put the dd command to good use.

How To Use The Dd Command On Linux Featured Image

dd is one of the most important commands in Linux, mostly used in backing up hard disks and partitions. When utilized correctly, dd can be a powerful tool for writing data from one partition to another and performing different tasks with files. Here we show you how to put the dd command to good use.

The Story of dd

The command was originally developed at AT&T Bell Laboratories in the 1970s by a computer scientist named Ken Thompson. It was developed for Unix-based systems and given a name that was chosen as a reference to a statement found in IBM’s Job Control Language, which was called “DD.” It can be noted that the syntax of the command closely resembles a statement in the Job Control Language.

The command was originally designed to be a utility to convert ASCII to EBCDIC and vice versa. It was first integrated with a version of Unix in 1974, with the release of the 5th Edition of the operating system.

dd has been called “disk destroyer” as a joke in the Unix community due to its ability to destroy data on hard disks when used improperly.

Basic Operands

Now that you know a little bit about the background of the command and its destructive abilities when used incorrectly, it’s time to learn how to take advantage of everything it offers to users of different Linux distributions.

Firstly, view the manual using the --help flag:

Dd Command Help Flag

There are two operands for the command that are most commonly utilized. They are if and of, which stand for “input file” and “output file” respectively. The if operand is used to represent the source location, while the of operand is used to represent the location where you intend to save the data from the input location.

The most common source and output locations include hard disks, partitions, and disk images.

Before using the command, it might be helpful to use the fdisk utility to view the partitions on your system. This can easily be done using the command’s -l flag:

Fdisk Command List Partitions

In this case, if is used to represent the “/dev/sda” drive, and of represents the “/dev/sdb” drive, where the data from “/dev/sda” will be saved to:

Dd Command Example 1

Creating a Disk Image

One of the best use cases for the command is creating disk images in the “.img” file format. This is extremely useful for backing up data on your Linux-based system and is likely the quickest and easiest way to back up an entire hard disk.

The logic here is primarily the same in this case, with the if operand representing the “/dev/sda” drive and the of operand representing an “.img” file, where the data from the hard disk will be saved to:

Dd Command Creating Disk Image

Saving a Disk Image to a Partition

Creating a disk image with the command is fairly straightforward, but so is the reversed version of that process.

In this scenario, our disk image file is acting as the input file, and our new partition is acting as the output file. The utility is saving the data from our disk image to our “/dev/sdb” partition:

Dd Command Save Image Partition

Creating a Compressed Disk Image

If you create a disk image of a full-size hard disk, you can imagine that the file size of the final disk image will likely be quite large. For this reason, the dd utility has a feature that creates compressed disk images.

A compressed disk image can be created by using the pipe | command. In this case, it is used to take the contents of the input file and perform the gzip command with a -c flag, with the contents being packaged into a “.gz” file:

Dd Command Compressed Disk Image

Specifying a Block Size

You can also play around with the speed of the dd command’s operation. This can be accomplished using the bs operand, which is used to represent block size. Block size represents the number of bytes dd copies to the output file in a single instance. It is represented using multiples of 1024 bytes, and the default value is set to 512 bytes. The higher the block size is the faster the data will be saved to the output file.

In this case, we are setting the block size to 2048:

Block size can also be specified in kilobytes:

Dd Command Blocksize

Wiping a Hard Disk

dd can also be used to wipe your hard disk. This is accomplished by reading zeroes or random characters from “/dev/zero” or “/dev/urandom” and saving them to the hard disk/partition, which overwrites the data on it. This is extremely useful when you want to make sure your data can’t easily be retrieved after you have sold or otherwise disposed of your hard disk.

Overwriting a hard disk using zeroes:

Dd Command Wiping Hard Disk Zeroes

The process of overwriting can also be done using random characters:

Dd Command Wiping Hard Disk Urandom 1

Creating a Bootable USB Drive

The creation of bootable USB flash drives using “.iso” files with the command is simple:

Dd Command Creating Bootable Usb

Wrapping Up

It is safe to say that the dd command can be considered a “Swiss Army knife” due to its usefulness in many areas and anything to do with hard drives, partitions, and disk image files. As long as you don’t destroy your hard disk, it’s an effective and simple-to-use tool for anything ranging from wiping a hard disk or a USB drive to creating compressed backups.

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox