Simple Linux Commands: The dd command
What is dd?
Disk Definition (dd) came from IBM’s Job Control Language and became a staple of the low-level UNIX and Linux commands. What dd does is copy bytes from an input source to an output source. But dd can also copy raw data as well, which makes it perfect for copying boot sectors.
The dd command can handle many tasks including:
disk cloning (Note: Target disk must be of equal size or greater than the original disk)
disk imaging
partition cloning
restoring from image file
The dd command is an incredibly powerful tool, so much so it can, if used improperly, do serious damage to data. To that end dd is often jokingly referred to as Disk Destroyer. So, when using dd, you want to use it with caution. In fact, it’s always best to practice using dd on non-production machines.
Basic command structure
The dd command uses a somewhat outdated command structure, relying on if and of statements. Think of if as input file and of as output file. And remember, Linux devices are seen in the form of /dev/sd* (Where * is the letter associated with a particular device and can often be followed by a single number). So the structure of the command (which must be run as the root user) looks like:
dd if=/dev/sda of=/dev/sdb
What the above command would do is clone the data located on /dev/sda to /dev/sdb. The dd command doesn’t really care what the contents of the drives are, just the locations.
Naturally there are arguments aplenty for dd. We’ll discuss those, as needed, in the examples below.
Cloning a drive
To clone a drive you would use the command:
dd if=/dev/sda of=/dev/sdb
Where /dev/sda is the location of the device to clone and /dev/sdb is the location of the target device. This command doesn’t care how your drive is partitioned, nor the type of data that is on the drive. What this will create is an exact copy of one drive onto another.
Creating an image file of a hard drive
An image file is a great way to save a compressed image of your hard drive. To do this issue the command:
dd if=/dev/sda /path/to/disk.img
Where /dev/sda is the device to clone and /path/to/disk.img is the location where you want the image to be saved. Once the image is complete you can compress the image (to save space) and copy it onto CD for safe keeping.
Cloning a partion of a drive
What if you only want to clone a single partition of a drive and save it as an image? To do this issue the command:
dd if=/dev/sda1 /path/to/disk_partition.img
Where /dev/sda1 is the partition to clone and /path/to/disk_partition.img is the location to save the image file.
Restoring from image
What if you need to restore a partition (or drive) from a saved image? Simply reverse the if and of statements like so:
dd if=/disk.img /dev/hda <– to restore an entire drive (Where /disk.img is the image to restore and /dev/hda is where the image is to be restored to.)
dd if=disk_partition.img /dev/hda1 <– to restore a single partition on a drive (Where disk_partition.img is the partition image to restore and /dev/hda1 is the location of the partition to be restored.)
Final Thoughts
The dd command is as powerful as it is useful. What you have read is only a cursory (as well as most useful) introduction to what dd can do. If you have need to image or clone Linux drives, dd is the tool you need.
This post is part of the series: Linux Command Line
If you ever plan on doing any administration on a Linux machine, you would be well served to get to know the command line interface. In this Bright Hub series you will be introduced to various concepts surrounding one of the most powerful admin tools around.
- Linux Command Line: Introduction
- Linux Command Line: ls
- Linux Command Line: cd
- Linux Command Line: mkdir
- Linux Command Line: df
- Linux Command Line: ln
- Linux Command Line: top
- Linux Command Line: mount/umount
- Linux Command Line: Cron/Crontab
- Linux Command Line: chmod
- Linux Command Line: wget
- Linux Command Line: cat
- Linux Command Line: grep
- Linux Command Line: dd
- Linux Command Line: sudo
- Linux Command Line: startx
- Linux Command Line: adduser
- Linux Command Line: at
- Linux Command Line: aterm
- Linux Command Line: nano
- Linux Command Line: hostname