How to pad a file to a desired size?

Besides the answers to get a physical padding you may also leave most of the padding space in the file just empty ("holes"), by seeking to the new end-position of the file and writing a single character:

dd if=/dev/zero of=largerfile.txt bs=1 count=1 seek=16777215

(which has the advantage to be much more performant, specifically with bs=1, and does not occupy large amounts of additional disk space).

That method seems to work even without adding any character, by using if=/dev/null and the final desired file size:

dd if=/dev/null of=largerfile.txt bs=1 count=1 seek=16777216

A performant variant of a physical padding solution that uses larger block-sizes is:

padding=262144 bs=32768 nblocks=$((padding/bs)) rest=$((padding%bs))
{
  dd if=/dev/zero bs=$bs count=$nblocks
  dd if=/dev/zero bs=$rest count=1
} 2>/dev/null >>largerfile.txt

Drop the of=largerfile.txt and append stdout to the file:

dd if=/dev/zero bs=1 count=262144 >> largerfile.txt

Simple answer, courtesy of @frostschutz, posted as an answer

truncate -s 16M thefile

Tags:

Dd