dd

Chris Torek chris at mimsy.UUCP
Fri Sep 9 11:26:47 AEST 1988


In an article whose referent has been deleted by bogus news software,
I wrote:
>>`dd' can indeed do the trick, but not with the options you gave. ...
>>	dd bs=N
>>and
>>	dd ibs=N obs=N
>>have different meanings.

In article <43200036 at uicsrd.csrd.uiuc.edu> kai at uicsrd.csrd.uiuc.edu writes:
>With NO options, dd just copies input to output.  If you specify "bs=20b", or
>"ibs=20b obs=20b", dd reads and writes 10240 byte blocks from input to
>output.  Sorry, but these two parameter settings are the same, except that
>using "bs" to specify both input and output buffer sizes is more efficient,
>since copying between buffers need not be done, unless you are doing ASCII to
>EBCDIC conversions or some other type of data conversion.

Not so.  (Qualifier: in V7, 2BSD, 3BSD and 4BSD, at least.)  I
discovered this by checking the source code.  Setting `bs' has the side
effect of not only setting `ibs' and `obs' but also setting an internal
flag called `fflag'.  If fflag is not set, dd will re-block input to
output, so that `dd ibs=N obs=N' guarantees that all output writes save
the last will be N bytes long.  If fflag is set, and no conversion is
needed, then it does indeed avoid copying between buffers, *and in
addition*, it writes as output blocks the number of bytes read with
each `read' system call.

For example, if I have a tape on drive 0 (as /dev/tape/0 at 1600) and one
on drive 1 (as /dev/tape/1 at 1600), and the one on drive 0 has four tape
records containing 512, 128, 1024, and 256 bytes, then the command

	dd if=/dev/tape/0 at 1600 of=/dev/tape/1 at 1600 bs=1024

produces on the tape on drive 1 four tape records containing 512, 128,
1024, and 256 bytes, while the command

	dd if=/dev/tape/0 at 1600 of=/dev/tape/1 at 1600 ibs=1024 obs=1024

produces on the tape on drive 1 two tape records containing 1024 bytes
and 896 bytes respectively.

You can experiment with tty input and output, which shares with tape
drives the peculiar property that the number of bytes returned from a
read syscall is not necessarily the number of bytes requested.  Run

	$ dd bs=4

and type as input

	1
	2
	3
	^D

Note that the output appears intermingled:

	1
	1
	2
	2
	3
	3
	0+3 records in
	0+3 records out
	$

Now try

	$ dd ibs=4 obs=4

and type as input

	1
	2
	3
	^D

This time the output appears thus:

	1
	2
	1
	2
	3
	3D		(this `D' is left over from the BSD tty driver ctlecho)
	0+3 records in
	1+1 records out
	$

This confirms that `dd bs=N' and `dd ibs=N obs=N' do different things.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris at mimsy.umd.edu	Path:	uunet!mimsy!chris



More information about the Comp.unix.wizards mailing list