dd
command.
The dd
tool has been a critical component on the Linux (and UNIX) command
line for ages. You know a command-line tool is important if it has only two
letters, and dd
is no exception. What I love about it in particular is that it
truly embodies the sense of a powerful tool with no safety features, as
described in Neal Stephenson's In the Beginning was the Command
Line. The dd
command does something simple: it takes input from one file and outputs it to
another file, and since in UNIX "everything is a file", that means
dd
doesn't
care if the output file is another file on your disk, a partition or even
your active hard drive, it happily will overwrite it! Because of this,
dd
fits in that immortal category of sysadmin tools that I type out and then
pause for five to ten seconds, examining the command, before I press Enter.
Unfortunately, dd
has fallen out of favor lately, and some distributions
even will advise using tools like cp
or a graphical tool to image drives. This is
largely out of the concern that dd
doesn't wait for the disk to sync before
it exits, so even if it thinks it's done writing, that doesn't mean all of
the data is on the output file, particularly if it's over slow I/O like in
the case of USB flash storage. The other reason people have tended to use
other imaging tools is that traditionally dd
doesn't output any progress. You
type the command, and then if the image is large, you just wait, wait and then
wait some more, wondering if dd
will ever complete.
But, it turns out that there are
quite a few different ways to get progress output from dd
, so
I cover a few popular ones here, all based on the following dd
command to image
an ISO file to a disk:
$ sudo dd if=/some/file.iso of=/dev/sdX bs=1M
pv
Like many command-line tools, dd
can accept input from a pipe and output to a
pipe. This means if you had a tool that could measure the data flowing over a
pipe, you could sandwich it in between two different dd
commands and get live
progress output. The pv
(pipe viewer) command-line tool is just
such a tool, so one approach is to install pv
using your distribution's packaging tool and
then create a pv
and dd
sandwich:
$ sudo dd if=/some/file.iso bs=1M | pv | dd of=/dev/sdX
In this command, I'm imaging my ISO image to a disk. Notice that the first
dd
command lists not only the if
argument to specify the input file, I also
added the bs
argument to this side. In general, you will want to add all of
your dd
arguments to the first dd
command.
kill
The dd
command has an often-forgotten feature buried within its man pages. If
you send a running dd
command a kill -USR1
signal, it will output its current
progress. So run the initial dd
command in this example, and then in a
different terminal, identify its process ID so you can send it the
USR1
signal:
$ sudo kill -USR1 <pidofddcommand>
You can use a bit of a shell shortcut if you don't want to identify the PID command independently and put this all in one line:
$ sudo kill -USR1 $(pgrep ^dd)
dd
's Embedded Progress Bar
Many people are unaware that relatively recently, dd
added its own live
progress option! For the longest time, I was using the USR1
trick until
someone told me about dd
's new status=progress
option added in GNU coreutils
8.24. So now, you just have to type:
$ sudo dd if=/some/file.iso of=/dev/sdX bs=1M status=progress
And, dd
will output its progress periodically while it's running!
—Kyle Rankin