Home > Basic Linux-related things, Linux > dd is not a disk writing tool

dd is not a disk writing tool

April 20th, 2015

If you’ve ever used dd, you’ve probably used it to read or write disk images:

# Write myfile.iso to a USB drive
dd if=myfile.iso of=/dev/sdb bs=1M

Usage of dd in this context is so pervasive that it’s being hailed as the magic gatekeeper of raw devices. Want to read from a raw device? Use dd. Want to write to a raw device? Use dd.

This belief can make simple tasks complicated. How do you combine dd with gzip? How do you use pv if the source is raw device? How do you dd over ssh?

The fact of the matter is, dd is not a disk writing tool. Neither “d” is for “disk”, “drive” or “device”. It does not support “low level” reading or writing. It has no special dominion over any kind of device whatsoever.

dd just reads and writes file.

On UNIX, the adage goes, everything is a file. This includes raw disks. Since raw disks are files, and dd can copy files, dd be used to copy raw disks.

But do you know what else can read and write files? Everything:

# Write myfile.iso to a USB drive
cp myfile.iso /dev/sdb

# Rip a cdrom to a .iso file
cat /dev/cdrom > myfile.iso

# Create a gzipped image
gzip -9 < /dev/sdb > /tmp/myimage.gz

However, this does not mean that dd is useless! The reason why people started using it in the first place is that it does exactly what it’s told, no more and no less.

If an alias specifies -a, cp might try to create a new block device rather than a copy of the file data. If using gzip without redirection, it may try to be helpful and skip the file for not being regular. Neither of them will write out a reassuring status during or after a copy.

dd, meanwhile, has one job*: copy data from one place to another. It doesn’t care about files, safeguards or user convenience. It will not try to second guess your intent, based on trailing slashes or types of files. When this is no longer a convenience, like when combining it with other tools that already read and write files, one should not feel guilty for leaving dd out entirely.

This is not to say I think dd is overrated! Au contraire! It’s one of my favorite Unix tools!

dd is the swiss army knife of the open, read, write and seek syscalls. It’s unique in its ability to issue seeks and reads of specific lengths, which enables a whole world of shell scripts that have no business being shell scripts. Want to simulate a lseek+execve? Use dd! Want to open a file with O_SYNC? Use dd! Want to read groups of three byte pixels from a PPM file? Use dd!

It’s a flexible, unique and useful tool, and I love it. My only issue is that, far too often, this great tool is being relegated to and inappropriately hailed for its most generic and least interesting capability: simply copying a file from start to finish.

* dd actually has two jobs: Convert and Copy. Legend has it that the intended name, “cc”, was taken by the C compiler, so the letters were shifted by one to give “dd”. This is also why we ended up with a Window system called X.

Basic Linux-related things, Linux

  1. Nobody
    | #1

    # cat /dev/cdrom > myfile.iso


    # cat myfile.iso > /dev/cdrom

    Won’t – dd allows you do handle writes in various block sizes, so if a device can’t handle a one-byte write, cat could well end up writing your byte followed by zeros (and because of where the pointer now is, do it again for the next byte).

    Still, all your other points stand – nice piece.

  2. Nician
    | #2

    You left out one of its other nifty uses. Writing changes to binary files.

  3. Harry
    | #3

    “If an alias specifies -a, cp might try to create a new block device rather than…”

    The above line is not clear, could please elaborate.

    Also, if dd has only 1 job (convert and copy) why can’t it be used to tar up a directory and its contents into a regular file (similar to what tar does)?

  4. Harry
    | #4


    Typo Correction: The above line is not clear, could [you] please elaborate?

  5. Blake Frederick
    | #5

    I had no idea! I was just blindly using dd. Thank you for this wonderful article.

  6. Paweł Lasek
    | #6

    DD is actually a pun on Data Definition I IBM’s JCL language that contemporary computer users would be quite possibly aware of.

  7. | #7

    Interesting read!

  8. | #8

    Thanks Vidar, I’ve been using dd to write usb boot disks for years without even thinking about it

  9. | #9

    You are absolutely right. I heard that dd was originally intended to be called “carbon copy”, but as you said, cc was already occupied.

  10. | #10

    The name dd likely comes from JCL SYSIN DD command, which was used for punch cards.
    Also – dd(1) is low level in that it can handle hardware read errors. Look at the source and you can see it re-tries when an error occurs.

  1. No trackbacks yet.