Linux Basics – dd and Devices

0
0

The program dd is extremely useful when working with block and character devices. This program’s sole function is to read from an input file or stream and write to an output file or stream, possibly doing some encoding conversion on the way.

dd copies data in blocks of a fixed size. Here’s how to use dd with a character device and some common options:

$ dd if=/dev/zero of=new_file bs=1024 count=1

As you can see, the dd option format differs from the option formats of most other Unix commands; it’s based on an old IBM Job Control Language (JCL) style. Rather than use the dash (-) character to signal an option, you name an option and set its value to something with the equals (=) sign. The preceding example copies a single 1024-byte block from /dev/zero (a continuous stream of zero bytes) to new_file.

These are the important dd options:

  • if=file The input file. The default is the standard input.
  • of=file The output file. The default is the standard output.
  • bs=size The block size. dd reads and writes this many bytes of data at a time. To abbreviate large chunks of data, you can use b and k to signify 512 and 1024 bytes, respectively. Therefore, the example above could read bs=1k instead of bs=1024.
  • ibs=sizeobs=size The input and output block sizes. If you can use the same block size for both input and output, use the bs option; if not, use ibs and obs for input and output, respectively.
  • count=num The total number of blocks to copy. When working with a huge file—or with a device that supplies an endless stream of data, such as /dev/zero—you want dd to stop at a fixed point or you could waste a lot of disk space, CPU time, or both. Use count with the skip parameter to copy a small piece from a large file or device.
  • skip=
  • num Skip past the first num blocks in the input file or stream and do not copy them to the output.

WARNING

dd is very powerful, so make sure you know what you’re doing when you run it. It’s very easy to corrupt files and data on devices by making a careless mistake. It often helps to write the output to a new file if you’re not sure what it will do.

 

LEAVE A REPLY

Please enter your comment!
Please enter your name here