gavd.co.uk

Frail and faltering follower of Jesus

Linux command of the day, 5 of 31 - dd

By Gavin Davies

I’m looking briefly at a Linux command every day for a month. Today: dd

I’m looking briefly at a Linux command every day for a month. Today: dd. This isn’t intended to be a tutorial, just some brief notes for fun

I used dd yesterday to generate a big file. Today I’ll explore “Data Duplicator” a little more.

I can convert a file to uppercase using dd. if is input file and of should be obvious as a result! ;-)

$  echo foo > bar
$  cat bar
foo
$  dd if=bar of=barup conv=ucase
0+1 records in
0+1 records out
4 bytes transferred in 0.000056 secs (71392 bytes/sec)
$  cat barup
FOO

OK so that’s moderately useful, but what else does dd do?

It has a parm skip that will skip _n_ blocks, where block size is a param ibs. ibs defaults to default block size for the device (I think).

So I can set block size to 1 to skip just the first character:

$ echo "foo bar baz\nbip" > bar ; dd if=bar of=barup conv=ucase skip=1 ibs=1 ; cat barup
15+0 records in
0+1 records out
15 bytes transferred in 0.000044 secs (341927 bytes/sec)
OO BAR BAZ
BIP

This can be useful for stripping characters I guess, but there are many ways to do that. The power of dd seems to me to be more low-level block manipulation.

For example, I can make a full copy of a partition:

dd if=/dev/hda1 of=~/partition.img

Or backing up a master boot record:

$ sudo dd if=/dev/sda bs=512 count=1 of=mbr.img

I can wipe data quickly using dd:

# overwrite sda device with 1megabyte blocks of null chars
sudo dd if=/dev/zero bs=1M of=/dev/sda

So, dd allows duplication and transformation of data. Like everything unix-y, you can pipe it, so you can gzip etc.