18

I've read many guides and forum posts describing how to use dd, but one thing I've noticed is that people always use different values for the bs=, count= and seek= switches.

Please can someone explain what these switches do exactly (the man page isn't very detailed), and explain what the best settings for them are for different tasks, such as creating files from /dev/random or /dev/zero, and overwriting partitions and external drives.

24

I really don't know how to explain this better than the manpage does.

bs= sets the blocksize, for example bs=1M would be 1MiB blocksize.

count= copies only this number of blocks (the default is for dd to keep going forever or until the input runs out). Ideally blocks are of bs= size but there may be incomplete reads, so if you use count= in order to copy a specific amount of data (count*bs), you should also supply iflag=fullblock.

seek= seeks this number of blocks in the output, instead of writing to the very beginning of the output device.

So, for example, this copies 1MiB worth of y\n to position 8MiB of the outputfile. So the total filesize will be 9MiB.

$ yes | dd bs=1M count=1 seek=8 iflag=fullblock of=outputfile
$ ls -alh outputfile
9.0M Jun  3 21:02 outputfile
$ hexdump -C outputfile
00000000  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00800000  79 0a 79 0a 79 0a 79 0a  79 0a 79 0a 79 0a 79 0a  |y.y.y.y.y.y.y.y.|
*
00900000

Since you mention /dev/random and overwriting partitions... it will take forever since /dev/random (as well as /dev/urandom) is just too slow. You could just use shred -v -n 1 instead, that's fast and usually available anywhere.

18

Ok, you said the man pages are not detailed, so I will explain what they mean with easy to understand metaphors about a moving guy (it goes by the name of dd):

   bs=BYTES
          read and write up to BYTES bytes at a time

dd picks up something (boxes, vases, beds, rice, etc.), move where it needs to be and drop it off. Until he doesn't drop the load, he doesn't pick anything else. Now, when you need to tell him exactly how many objects he should pick per travel, this is what bs do. You set the amount of data he will read and write. This is almost mandatory in all useful and common commands.

   count=N
          copy only N input blocks

This determinate the total amount of boxes he will move. Boxes in this context are the blocks on the disk. You tell him to move 5 boxes, he only move 5 boxes even if there is more than 5 boxes (if there are less than 5 boxes, he will take a vase he found besides the boxes to add it up). If you tell dd to count only 5, and write it somewhere, he would copy the first 5 blocks he sees and write them where you want.

   seek=N skip N obs-sized blocks at start of output

The guy normally finds the first available place to drop the load, this is normally at the start (of the disk), and continue filling up until the end. Well, with this you tell dd to start further up, say instead of the hall, start in one of the rooms further inside. It just "skip" the starting blocks.

Now, depending what you are doing, you will need different combinations based on source and destination, along with the format they will be read and written. I recommend you to search for them separated.

  • "he will take a vase he found besides the boxes to add it up". What does this metaphor stand for? – Ini Dec 25 '18 at 19:43
  • 1
    @Ini That if there's a adjacent block that doesn't belongs to the if read, dd will read it and move it too. A vase isn't a box, yet dd moves it. – Braiam Dec 25 '18 at 20:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.