Hidden Linux : Example commands - dd (Part I)
Despite its dull-sounding description -- "Convert and copy a file"
-- dd is one of the most
powerful tools in the Linux toolbox. That's because in Linux, everything is a file! I should warn
you however that dd can also be d-d-dangerous if you get your ifs and ofs mixed up. But more on that in a
moment.To get a feel for what it can do, imagine you have a USB drive you want to temporarily use for something else. Running df -h shows it's mounted on /dev/sdc. The commmand...
dd if=/dev/sdc
of=/path/to/image bs=4096 |
...will do just that. To write the image back to the drive you'd use...
dd if=/path/to/image of=/dev/sdc
bs=4096 |
Notice how similar those commands are? We've just swapped the if= and the of=. That's what makes dd dangerous. If you mixed them up, in this example you'd write the new contents of your USB disk over the old image file, essentially nuking it. Be careful!
(By convention, if= comes first, but there's nothing to stop you using dd of=/dev/sdc if=/path/to/image bs=4096 for the restore step.)
The bs=4096, by the way, is optional. dd reads and writes 512 bytes at a time by default. Increasing this will speed up writes to the USB drive.
Since we're talking about syntax, here's a brief overview. (For more details type dd --help.)
| if= |
Input file. |
| of= | Output file. |
| bs= |
Bytes to read and write at a time. The default is 512. |
| count= |
Copy this number of blocks from the input file (instead of everything until the end of the file is reached). |
| conv= |
Convert the file as specified in the following comma-separated arguments. |
| noerror |
Continue even if read errors are encountered. (dd will normall stop.) |
| notrunc | Don't truncate the output file. |
Create an ISO image of a CD or DVD
dd if=/dev/cdrom
of=/path/to/image.iso bs=2048 conv=sync,notrunc |
The ISO image can then be used in a regular CD/DVD burner, or you can mount and access it locally with these commands...
mkdir /mnt/temp/path/to/image.iso /mnt/temp/ |
MBR Tricks
Backup the Master Boot Record (MBR) including the partition table:
dd if=/dev/sdx
of=/path/to/image count=1 bs=512 |
Backup the MBR without the partition table:
(Partition tables are unique to each disk so unless you're cloning a whole drive you shouldn't overwrite them.)
dd if=/dev/sdx
of=/path/to/image count=1 bs=446 |
Restore the MBR:
dd if=/path/to/image
of=/dev/sdx |
Backing Up Drives
dd makes cloning drives easy, but you shouldn't do it with either drive mounted! Boot from a Linux startup CD and run the command from there.
Backup an existing drive:
dd if=/dev/sdx of=/dev/sdy
|
Backup a hard disk to an image file:
dd if=/dev/sdx
of=/path/to/image |
Backup a hard disk to a compressed image file:
dd if=/dev/sdx | gzip >
/path/to/image.gz |
Restore a backup image:
dd if=/path/to/image
of=/dev/sdx |
Restore a compressed backup image:
gzip -dc /path/to/image.gz
| dd of=/dev/sdx |
Backup a partition to a different partition on another drive:
dd if=/dev/sdx2
of=/dev/sdy5 bs=4096 conv=notrunc,noerror |
Nuking a Disk
You don't have to write meaningful data to a disk, of course. You can use dd to wipe a disk too.
Wipe an entire disk by filling it with random data:
dd if=/dev/urandom
of=/dev/sdx conv=notrunc |
Wipe an entire disk by filling it with zeros:
dd if=/dev/zero
of=/dev/sdx conv=notrunc |
There are some even more cunning tricks you can do with dd. More next time!
![]() |
![]() |



PC World is New Zealand’s top selling computing and technology magazine.
Comments
Hi There,
I was looking for below solution and tried in my own way though it was working it took such a long time. Thank you so much.
""Backup a hard disk to a compressed image file:
dd if=/dev/sdx | gzip > /path/to/image.gz ""
Cheers,
Venkat Raman K
Posted by: venkat | February 21, 2011 7:28 PM
Actually if you zero the remaining space first:
dd if=/dev/zero of=~/trash.file
rm trash.file
Then use the commands as shown for backing up a partition by piping it through a compression utility you will end up with a very tiny backup file. I have used it many times to back up fresh installs of OS's. Just plugin a big enough external drive and you can refresh your PC/laptop to factory condition.
I've got instructions for doing it with bzip here: http://blog.pluis.com.au/2008/03/05/how-to-back-up-a-windows-installation/
Slightly different but the methodology is the same.
Posted by: Jaso | May 31, 2010 1:20 PM
dd is an extremely useful and powerful command. The only drawback creating an image of a hard disk is that it creates an image file the size of the whole partition, not just the used space on the partition. So, for example, if you have a 250GB partition that contains 1GB of data files, the resulting image file will be 250GB in size. Even using the gzip tip above, it will depend on what the physical data on the 'blank' portion of the partition contains as to how small the resulting image file will be.
Posted by: Adrian | May 25, 2010 2:10 PM