Writing files with dd

Writing files with dd

Hello,

like the SSH config post this is going to showcase two or three commands which I use very often when I’m in need of creating a file with a specific size.

To acheive this the command used on pretty much all Linux distribution is “dd”. dd is designed to copy blocks of data from an input file to an output file.

Let’s start with a few examples:

this first command is used to write a 1 Gb file using dd:

dd if=/dev/zero of=1g.img bs=1 count=0 seek=1G

The arguments are:

  • if= -> if the interface is present, this is the source of the data.
  • of= -> the name and path of the file to write
  • bs= -> the block size which is read each time
  • count= -> number of blocks to write
  • seek= -> skips n° blocks from the beginning of the file

Let’s look at another example, writing 10Mb:

dd if=/dev/zero of=10m.img bs=1 count=0 seek=10M
#or
dd if=/dev/zero of=10m.img bs=10M count=1 

The source can be changed depending on what kind of data you need:

  • /dev/zero : writes zeros
  • /dev/random : writes random data
  • /dev/urandom : writes more secure random data

One final thing to know about dd is that it can also be used to write images. For example, when you need to create a bootable USB with a Linux image. Let’s see how:

#first download your image...
wget http://cdimage.debian.org/debian-cd/8.5.0/amd64/iso-cd/debian-8.5.0-amd64-netinst.iso
#then write it to USB
dd if=/path/to/debian-8.5.0-amd64-netinst.iso of=/dev/sdbX

And that’s it! You have now a bootable USB with a Debian image.

Cheers,

Vincenzo


Comments are closed.