dd with obs and seek makes file of unexpected size

Your command dd if=/dev/zero of=./foo count=1 bs=1 obs=9 seek=1 creates a two-byte file rather than a 10-byte file because of poorly-defined interaction between bs and obs. (Call this a program bug if you like, but it's probably better defined as a documentation bug.) You are supposed to use either bs or ibs and obs.

Empirically it appears that bs overrides obs, so what gets executed is dd if=/dev/zero of=./foo count=1 bs=1 seek=1, which creates a two-byte file as you have seen.

If you had used dd if=/dev/zero of=./foo count=1 ibs=1 obs=9 seek=1 you would have got a 10-byte file as expected.

As an alternative, if you want to create an empty file that doesn't take any data space on disk you can use the counter-intuitively named truncate command:

truncate --size=10 foo

The POSIX manpage states:

ibs=expr

Specify the input block size, in bytes, by expr (default is 512).

obs=expr

Specify the output block size, in bytes, by expr (default is 512).

bs=expr

Set both input and output block sizes to expr bytes, superseding ibs= and obs=. If no conversion other than sync, noerror, and notrunc is specified, each input block shall be copied to the output as a single block without aggregating short blocks.

Linux's dd works the same way. Thus, use ibs instead:

dd if=/dev/zero of=./foo count=1 ibs=1 obs=9 seek=1

Tags:

Linux

Dd