Date: February 9, 1998

dd stands for Disk Dump. Or if it doesn't it should. The "main" use for dd is to duplicate a floppy disk, bit for bit, to a file. You probably used it to create boot disks when you installed Linux for the first time, unless you used its much less functional cousin rawrite. If you're sick of keeping boxes of floppies around, you can use dd in reverse, and throw the floppy away. Depending on permissions, you might have to do this as root.

dd if=/dev/fd0 of=quicken_install_disk_1.img bs=1440k

The if argument specifies an input file (which defaults to the standard input). Naturally, the of argument names the output file (which defaults to the standard output). Finally, the bs argument tells dd what block size to use. Here we set the block size equal to the size of a floppy disk, and let dd read one block of data.

The man page says that the purpose of dd is to "convert a file while copying it." In English, that means that dd does not assume a file is made of text! It doesn't look for carriage returns to delimit lines, it doesn't stop reading at the first binary zero, nothing! This gives us the power to read files exactly, byte for byte. It allows us to read a fixed number of bytes, or physically to overwrite a file.

As just one example, consider /dev/random. That's a nifty Linux innovation--a pseudo device that accumulates randomness. Would you like to read 10 bytes of random data from /dev/random? It's a snap.

dd if=/dev/random of=/tmp/random.bin bs=1 count=10

Note that /dev/random provides binary data, so if we omit the of argument then that data will probably trash our display. Alternately, we could have omitted the of argument, but piped the output through cat -v to escape any non-printable characters. In addition to the arguments explained above, we use the count argument to specify the number of blocks to read. In conjunction with a blocksize of 1, count=10 tells dd to read exactly 10 bytes.

Here's a final example, for the paranoid. When you delete a file using rm, you only delete the inode pointing to your data. The data is still there, on the disk, waiting for somebody with a "Disk Doctor" utility to resurrect and read. Does that bother you? Well, you should delete your data, not just your file. Again, dd comes to the rescue. Normally dd truncates its output file before writing. The argument conv=notrunc overrides that behavior, and causes dd to write over any existing data. The following shell script combines all of these ideas, and wipes out your file by overwriting it five times with pseudorandom data, and then deleting it.

#!/bin/sh
FILE=$1
SIZE=`ls -l $FILE | awk -- '{print \$5;}'`
{
    for iteration in 1 2 3 4 5
    do
        dd if=/dev/urandom of=$FILE bs=${SIZE} count=1 conv=notrunc
        sync
    done
} && rm -f $FILE

Enjoy!
Len.

 

Top


Len Budney
lbudney@pobox.com
Copyright © 1998 - 2004
Page generated: 20:02:33 21-Dec-2004