Welcome to Dream.In.Code
Getting Help is Easy!

Join 95,475 Programmers for FREE!. Ask your question and get quick answers from Dream.In.Code experts. There are 971 online right now! We're the #1 programming help community on the internet! Registration is fast and FREE... Join Now!

Chat LIVE With a Expert

Register to Make This Box Go Away!


dd, it's usage & abilities.

 
Reply to this topicStart new topic

> dd, it's usage & abilities., Using dd for low-level copying and conversion of data & files

no2pencil
Group Icon



post 3 Oct, 2007 - 09:54 AM
Post #1


Before you try ANY of the following, please read the dd man page. With that said...

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:
QUOTE

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.
CODE

    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):
CODE

    dd if=/dev/fd0 of=floppy.img

Or to copy the img file back to a floppy:
CODE

    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):
CODE

    dd if=/dev/random of=reallylargefile count=2M

or faster but less cryptographicly secure
CODE

    dd if=/dev/urandom of=reallylargefile count=2M

To make a swap file, or another swapfile on a running system:
CODE

    dd if=/dev/zero of=/swapspace bs=4k count=250000
    mkswap /swapspace
    swapon /swapspace

To Completly blank a disk
CODE

    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):
CODE

    dd if=/dev/dsp of=/dev/hda

Or, to benchmark your hard drive, use the time command:

Read Time:
CODE

    time dd if=/home/sam/1Gb.file bs=64k | dd of=/dev/null

Write Time:
CODE

    time dd if=/dev/zero bs=1024 count=1000000 of=/home/sam/1Gb.file

Your output will look similar to this:
QUOTE

1000000+0 records in
1000000+0 records out

real 2m17.951s
user 0m0.930s
sys 0m25.160s

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:
CODE

    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:
CODE

    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:
CODE

    dd if=/dev/zero of=/dev/ram7 bs=1k count=16384

Make a 16 MB ramdisk full of zeroes.
CODE

    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.



Register to Make This Ad Go Away!

ljfox4
*



post 28 Apr, 2008 - 03:41 PM
Post #2
Excellent tutorial. I actually have found myself to be using this as a reference. It is a very powerful command indeed.


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 7/5/08 03:45AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month
-->