You'll often see instructions for creating and using disk images on Unix systems
making use of the dd
command. This is a strange program
of [obscure provenance](https://en.wikipedia.org/wiki/Dd_(Unix)) that somehow,
still manages to survive in the 21st century.
Actually, using dd
is almost never necessary, and due to its highly
nonstandard syntax is usually just an easy way to mess things up. For instance,
you'll
see
instructions like this asking
you to run commands like:
# Obscure dd version
dd if=image.iso of=/dev/sdb bs=4M
Guess what? This is exactly equivalent to a regular shell pipeline using cat
and shell redirection:
# Equivalent cat version
cat image.iso >/dev/sdb
That weird bs=4M
argument in the dd
version isn't actually doing anything
special---all it's doing is instructing the dd
command to use a 4 MB buffer
size while copying. But who cares? Why not just let the command figure out the
right buffer size automatically?
Another reason to prefer the cat
variant is that it lets you actually string
together a normal shell pipeline. For instance, if you want progress information
with cat
you can combine it with the pv
command:
# Cat version with progress meter
cat image.iso | pv >/dev/sdb
There's an obscure option to GNU dd
to get it to display a progress meter as
well. But why bother memorizing that? If you learn the pv
trick once, you can
use it with any program.
If you want to create a file of a certain size, you can do so using other
standard programs like head
. For instance, here are two ways to create a 100
MB file containing all zeroes:
# Obscure dd version
dd if=/dev/zero of=image.iso bs=4MB count=25
# Regular head version
head -c 100MB /dev/zero >image.iso
The head
command is useful for lots of things, not just creating disk images.
Therefore it's a better investment of your time to learn head
than it is to
learn dd
. In fact, you probably already know how to use it.
I will confess: there are some interesting options that dd
has, which aren't
easily replicated with cat
or head
. For instance, you can use dd
to
convert a file between ASCII and EBCDIC
encodings. So if you find yourself doing that a lot, I won't blame you for
reaching for dd
. But otherwise, try to stick to more standard Unix tools.