Generate File of a certain size?

You can use dd:

dd if=/dev/zero of=output.dat  bs=24M  count=1

or

dd if=/dev/zero of=output.dat  bs=1M  count=24

or, on Mac,

dd if=/dev/zero of=output.dat  bs=1m  count=24

Under non-embedded Linux or Cygwin (or any system with GNU coreutils) and FreeBSD:

truncate -s 24m example.file

This creates a file full of null bytes. If the file already exists and is smaller, it is extended to the requested size with null bytes. If the file already exists and is larger, is is truncated to the requested size.

The null bytes do not consume any disk space, the file is a sparse file.

On many systems, head -c 24m </dev/zero >example.file creates a non-sparse file full of null bytes. If head doesn't have a -c option on your system (it's common but not in POSIX), you can use dd bs=1024k count=24 </dev/zero >example.file instead (this is POSIX-compliant).


If you don't care about the content of the file, this is much faster than using dd:

fallocate -l 24M filename

Obviously, using dd for a 24MB file won't take any time on a modern system, but larger files can be noticeably slow.