How to use the dd command in Linux
dd
is one of the most important commands in Linux, mainly used for backing up hard drives and partitions. When used correctly, dd can be a powerful tool for writing data from one partition to another and performing various tasks with files. Here we show you how to use the dd command wisely.
dd’s story
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 was given a name that was chosen as a reference to a statement found in IBM’s Job Control Language, which was called “DD”. Note that the command syntax closely resembles an instruction in the Job Control Language.
The command was originally designed to be a utility for converting ASCII to EBCDIC and vice versa. It was first incorporated into a version of Unix in 1974, with the release of the 5th edition of the operating system.
dd has been called a “disk destroyer” as a joke in the Unix community due to its ability to destroy data on hard drives when used incorrectly.
Basic operands
Now that you know a bit about the context of the command and its destructive abilities when used incorrectly, it’s time to learn how to take advantage of all it has to offer users of different Linux distributions.
First, consult the manual using the --help
flag:
There are two operands for the command that are most commonly used. They are if
and of
, which stand for “input file” and “output file” respectively. the if
the operand is used to represent the location of the source, while the operand of
The operand is used to represent the location where you intend to save the data from the input location.
dd if=<input file> of=<output file>
The most common source and output locations include hard drives, partitions, and disk images.
Before using the command, it may be useful to use the fdisk
utility to view the partitions on your system. This can easily be done using the command -l
flag:
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:
dd if=/dev/sda of=/dev/sdb
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 system and is probably the fastest and easiest way to back up an entire hard drive.
The logic here is essentially the same in this case, with the if
operand representing the “/dev/sda” drive and the of
operand representing an “.img” file, where the hard disk data will be saved:
dd if=/dev/sda of=example.img
Saving a disk image to a partition
Creating a disk image with the command is fairly straightforward, but so is the reverse version of this process.
In this scenario, our disk image file acts as the input file and our new partition acts as the output file. The utility saves our disk image data to our “/dev/sdb” partition:
dd if=example.img of=/dev/sdb
Creating a Compressed Disk Image
If you are creating a disk image of a normal sized hard drive, you can imagine that the file size of the final disk image will probably be quite large. For this reason, the dd utility has a feature that creates compressed disk images.
A compressed disk image can be created using the pipe |
order. In this case, it is used to take the contents of the input file and perform the gzip
order with a -c
flag, the content being grouped in a “.gz” file:
dd if=/dev/sda | gzip -c > image.gz
Specifying a block size
You can also play with the execution speed of the dd command. This can be accomplished using the bs
operand, which is used to represent the block size. The 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 larger the block size, the faster the data will be written to the output file.
In this case, we set the block size to 2048:
dd if=/dev/sda of=example.img bs=2048
The block size can also be specified in kilobytes:
dd if=/dev/sda of=example.img bs=2048K
Clean up a hard drive
dd can also be used to erase your hard drive. This is accomplished by reading zeros or random characters from “/dev/zero” or “/dev/urandom” and writing them to the hard drive/partition, which overwrites the data on it. This is extremely useful when you want to ensure that your data cannot easily be recovered after selling or otherwise disposing of your hard drive.
Overwrite a hard drive using zeros:
dd if=/dev/zero of=/dev/sdb
The overwrite process can also be performed using random characters:
dd if=/dev/urandom of=/dev/sdb
Creating a Bootable USB Drive
Creating bootable USBs using “.iso” files with the command is simple:
dd if=os.iso of=<USB drive location>
Wrap
It’s safe to say that the dd command can be considered a “Swiss army knife” because of its usefulness in many areas and with anything related to hard drives, partitions, and disk image files. As long as you don’t destroy your hard drive, it’s an effective and easy-to-use tool for everything from erasing a hard drive or USB flash drive to creating compressed backups.
Was this article helpful?
Comments are closed.