6

I'm curious to know the difference between these two commands in Linux:

$ cat ./boot.bin ./kernel.bin /dev/zero | dd bs=512 count=2880 of=devos.img

and

$ cat ./boot.bin ./kernel.bin > devos.img
muru
  • 72,889

1 Answers1

14

dd copies exactly count blocks of bs bytes, or 2880*512 bytes in total in this case(but see below). That will truncate or pad the concatenation of the two files to a fixed size (since /dev/zero gives as many zero bytes as required). 1440 kB is looks like the size of a 3.5" HD floppy disk, so perhaps someone wanted to make images that fit the floppy exactly.

The plain cat on your second example will just concatenate the files, and the result will of whatever size it is.

Smaller example:

$ echo hello > a; echo world > b
$ cat a b | od -c
0000000   h   e   l   l   o  \n   w   o   r   l   d  \n
$ cat a b /dev/zero | dd bs=1 count=8 2>/dev/null | od -c
0000000   h   e   l   l   o  \n   w   o
$ cat a b /dev/zero | dd bs=1 count=16 2>/dev/null | od -c
0000000   h   e   l   l   o  \n   w   o   r   l   d  \n  \0  \0  \0  \0

Actually, dd will read and write less if it gets less data than the block size on a single read() call. This might happen with large block sizes but probably isn't an issue with 512 since cat will write the data in blocks of at least that size. In GNU dd, this can be prevented with iflag=fullblock.

We could do the same with head -c:

$ cat a b /dev/zero | head -c 16 ...
ilkkachu
  • 138,973
  • Would the plain cat risk, say, leaving unused data on the disk, and possible corruption if that code ends up executed? –  Jul 05 '18 at 18:28
  • @NicHartley, yeah, if you had an image saved to a floppy, and then wrote another one that was shorter, it would leave remains of the first image there. But the new image would need to be buggy to try to read past the length of the files. It's not like zero-padding would help much in that case either, it's not very useful as code anyway... – ilkkachu Jul 05 '18 at 19:04