Post Reply 
[How To] [*nix] Using dd for dummies
Author Message
Ge64
Former Admin ;)

Posts: 3,163.4170
Threads: 295
Joined: 3rd Feb 2007
Reputation: 0.38918
E-Pigs: 108.4795
Offline
Post: #1
[How To] [*nix] Using dd for dummies
This is a how to that will apply to most unix-based platforms, i.e. Linux and Mac OS X being the most mainstream ones.

dd is a terminal tool that can copy data. What makes it different from, for example, cp, which copies files, is that it just copies the bytes and it can copy and convert all sorts of things. I have been playing around with it for a while and I'll post some very, very basic stuff here.

Making a file with dd:

dd takes an input, and puts it into an output. In order for it to make an output, though, it needs you to specify a block size (bs) and a number of blocks (count). You'll need this for the following things

Saving and opening stuff with dd:

dd by default takes input from the terminal and outputs it to the terminal. However, you can specify an input file (if) and output file (of). This way you can write files to the terminal window, or take input from the terminal into a file. To make a file with some text in it, just use the following. Specify enough bullchocolate and count to accommodate anything you're going to put in.

dd of=foo bs=4 count=8
type something here
and hit ctrl-c. Ctrl+C will terminate the program so that the file 'foo' now contains the text 'type something here'.

Copying a file with dd:

The input and output file options of dd let you copy files as well, simply use the following line to copy foo to bar, overwriting bar if it's there:

dd if=foo of=bar

Simple text conversions with dd:

dd lets you specify some conversions using the 'conv' operand. For more info, just view the manual page by typing 'man dd' in your terminal or googling that. I'll explain how one works here: conv=ucase. This will basically take whatever text is in the input, and make it uppercase. So let's take our foo file from above and display it in uppercase:

dd if=foo conv=ucase

Make an empty file with dd:

So, as you might have noticed, you can (among others) make files with dd and put stuff in it. An empty file is full of null bytes. Where do wee get null bytes from? Well, unix has an unlimited supply of null bytes in /dev/null. So to make an empty file, simply make up a block size and count for it to specify the size, and do this:

dd if=/dev/null of=1m-nullfile bs=1024 count=1024

The above will create an empty file of 1mb. You can also create randombytes using /dev/random instead of /dev/null, and there are more of these to explore.

Copy a part of a file with dd:

dd is capable of seeking through the input for x blocks before outputting it. Let's take our file foo and make a file bar from it that just says ' something here':

dd if=foo of=bar ibs=4 iseek=1
dd if=bar

As you may have picked up, wee tell dd to split the input into blocks of 4 bytes using ibs=4, and then skip one input block using iseek=1. Wee can also just copy 'type':

dd if=foo of=bar ibs=4 count=1
dd if=bar

Now that you know some of the very basics of dd, let's explore some more exciting, useful applications of it

Cloning a disk with dd:

As you've seen before, wee can use a device file as input for dd. Wee can also use it for output though. In this way, you can clone a disk without every mounting it. Imagine two identical flash disks, disk2 and disk3. To copy all the contents, including the entire partition table from disk2 to disk3, use:

dd if=/dev/disk2 of=/dev/disk3

Making backups with dd:

Making a complete image of a drive can be done with dd too, no need for norton ghost! Simply:

dd if=/dev/hdb | gzip > copyofhdb.gz

The above will give you a compressed and complete image of your harddrive hdb. You should always use some compression (in this case gzip) with this type of image, because otherwise dd will just make a file as big as the drive it's reading from. With this compression, the file should never be bigger as the used space on the drive, and if you have some duplicate files it will even be significantly smaller.

The reverse for this is:

gzip -dc copyofhdb.gz | dd of=/dev/hdb

Notice the pipe. I'll explain that another time.

Hiding a file inside another file with dd:

According to the manpage here you can append a file using oflag=append, however the version of dd supplied with Mac OS X doesn't support this and Macports doesn't have dd. So, with the right version of dd, you can hide a file inside another one by simply taking one and appending it to the other:

dd if=omgsecrets of=innocentimage.gif oflag=append

If you remember the size of innocentimage.gif before you did this, you can retrieve omgsecrets:

dd if=innocentimage.gif of=omgsecrets ibs=1 count=<size of host file>

Whenever somebody sees the .gif file, it will act normally in most cases because the file is terminated, so any reader will not look further than the EOF and disregard the omgsecrets appended to it.

Fixing an ipod after you repartitioned it with dd:

Well, you probably won't use this one, but I tried to use my 2nd gen nano as a harddrive and completely reformatted it wholly. iTunes doesn't really like it when you do that and I couldn't restore it because of an error, I guessed I had deleted a firmware partition. Well, I had, because I got hold of another of the same model and simply used dd to make an image of the ipod, and restore it to my own. And what do ya know, it works perfectly! I'm not done yet though, cause the other ipod was a 4gb one and mine is 8gb but iTunes now says 4GB, so I guess I need to resize the partition. If there's enough people who want me to I'll do a similar howto on diskutil, the powerful Mac OS partitioner.

That's it! Find any errors or have any good ideas, feel free to comment!

[Image: ub1985584.jpg]
(This post was last modified: 11/03/2008 04:16 PM by Ge64.)
11/03/2008 03:44 AM
Find all posts by this user Quote this message in a reply
Method
You may call me Reverend.

Posts: 6,358.2856
Threads: 443
Joined: 14th Jan 2008
Reputation: 6.04241
E-Pigs: 71.3136
Offline
Post: #2
RE: [How To] [*nix] Using dd for dummies
Kool you finally told us how to do it!!!! but wait... I don't have a Mac or Linux :(

WOW this must have taken ages.....


[Image: mvg1hw.gif]
(This post was last modified: 11/03/2008 04:02 AM by Method.)
11/03/2008 03:50 AM
Visit this user's website Find all posts by this user Quote this message in a reply
Ge64
Former Admin ;)

Posts: 3,163.4170
Threads: 295
Joined: 3rd Feb 2007
Reputation: 0.38918
E-Pigs: 108.4795
Offline
Post: #3
RE: [How To] [*nix] Using dd for dummies
Ima try and make a backup of a school computer today with my little linux harddrive :P see if I have enough time to let it run

[Image: ub1985584.jpg]
11/03/2008 04:28 PM
Find all posts by this user Quote this message in a reply
Post Reply 


Forum Jump:


User(s) browsing this thread: 1 Guest(s)

 Quick Theme: