dd is a Unix command used for the low-level copying and conversion of data & files. It can be used with pipes, as well as redirection. It is commonly used to copy regions of raw device files, e.g. backing up the boot sector of a hard disk, or to read fixed amounts of data from special files (located in /dev). It is important to remember that your devices may not follow the same naming scheme used in these examples. If you do not understand the device names, or their usage & syntax, then I suggest in the strongest manor that you DO NOT continue, as these commands used improperly can leave your data un-usable. Your only option will be to reformat & reinstall. You stand the possible to loose everything on your drive!
The main differance between dd & cp is that dd works byte for byte. Data from deleted files that may still be present on a disk are not visible through the file system, & will not be seen by cp.
The command line syntax of dd is unlike that of most Unix programs, using option=value rather than the standard -option value format.
The usage syntax is as follows:
dd if=<source in file> of=<target out file> bs=<byte size>(512, 1024, 2048, 4096, 8192, 16384, can be any reasonable number.) skip= seek= conv=<conversion>
The source is the data being read, & the target is where the data gets written.
To create an ISO image file from a CD. Insert the source cd and unmount it first if auto CD mount is enabled, this is to improve performance by preventing random access to the mounted filesystem.
dd if=/dev/cdrom of=/tmp/image.iso bs=2k
To create an image file named floppy.img of a floppy disk in the drive whose block-device name is /dev/fd0 (as the first floppy device is on Linux):
dd if=/dev/fd0 of=floppy.img
Or to copy the img file back to a floppy:
dd if=floppy.img of=/dev/fd0 bs=18k
To create a file filled with random data, with a size of 1 gig, do this (1G = 1073741824, 1073741824 / 512 (the default block size) = 2097152 = 2M):
dd if=/dev/random of=reallylargefile count=2M
or faster but less cryptographicly secure
dd if=/dev/urandom of=reallylargefile count=2M
To make a swap file, or another swapfile on a running system:
dd if=/dev/zero of=/swapspace bs=4k count=250000 mkswap /swapspace swapon /swapspace
To Completly blank a disk
dd if=/dev/zero of=/dev/hda
To completely corrupt an entire hard disk (/dev/dsp is the sound player/recorder & /dev/urandom can be used in place of /dev/dsp):
dd if=/dev/dsp of=/dev/hda
Or, to benchmark your hard drive, use the time command:
time dd if=/home/sam/1Gb.file bs=64k | dd of=/dev/null
time dd if=/dev/zero bs=1024 count=1000000 of=/home/sam/1Gb.file
Your output will look similar to this:
1000000+0 records in
1000000+0 records out
1000000+0 records out
How to rejuvenate a hard drive
This will sometimes cure input/output errors experienced when using dd. Over time the data on a drive, especially a drive that hasn't been used for a year or two grows into larger magnetic flux points than were originally recorded. This makes it hard for the drive heads to decipher these magnetic flux points. This results in read/write errors. Also, sometimes sector 1 goes bad resulting in a useless drive. What you need to do is:
dd if=/dev/sda of=/dev/sda
This process will rewrite all the data on the drive in nice tight magnetic patterns that can then be read properly.
Or, you can copy one hard disk partition to another hard disk, to create a backup:
dd if=/dev/sda2 of=/dev/sdb2 bs=4096 conv=notrunc,noerror
Make a ramdrive:
The Linux kernel usually makes a number a ramdisks you can make into ramdrives. You have to populate the drive with zeroes:
dd if=/dev/zero of=/dev/ram7 bs=1k count=16384
Make a 16 MB ramdisk full of zeroes.
mke2fs -m0 /dev/ram7 4096
I actually ran a website on a ramdrive for a while. The main benefit being the site ran extremely faster, however it was volatile, so I had to perform backups often or risk loosing everything.