How to create test files of any size

Commands for Linux, macOS and Windows that produce a file of an exact byte count, and when to use each.

Published by SampleTestFiles. About 3 minutes to read.

Sometimes you need a size that nobody publishes: 3 MB plus one byte, or 250 MB. Every operating system can create a file of an exact length with one command. The catch is that there are three different kinds of "file of size N", and they behave differently in tests.

Three kinds of generated file

  • Sparse files have a length but occupy almost no disk space; the file system records that the content is all zeros without storing it. They are created instantly at any size.
  • Zero-filled files really contain N zero bytes on disk. They compress to almost nothing.
  • Random files contain N unpredictable bytes. They take a moment to create and cannot be compressed.

For checking a size limit, any of the three works. For measuring upload or download speed, only random data gives an honest result, because compression anywhere on the path shrinks zeros to nothing. For filling a disk or a quota, avoid sparse files, which do not consume the space they claim.

Linux

Random data, exactly 10 MB:

head -c 10485760 /dev/urandom > random-10mb.bin

The same with dd, as ten blocks of 1 MiB:

dd if=/dev/urandom of=random-10mb.bin bs=1M count=10

A sparse file, created instantly:

truncate -s 10M sparse-10mb.bin

A file with its space actually reserved on disk:

fallocate -l 10M allocated-10mb.bin

In truncate and fallocate, the suffix M means 1,048,576 bytes and MB means 1,000,000 bytes.

macOS

head works as it does on Linux:

head -c 10485760 /dev/urandom > random-10mb.bin

With dd, note the lower-case unit on macOS:

dd if=/dev/urandom of=random-10mb.bin bs=1m count=10

A zero-filled file:

mkfile 10m zeros-10mb.bin

Windows

A zero-filled file of an exact byte count, from Command Prompt or PowerShell:

fsutil file createnew zeros-10mb.bin 10485760

Random data in PowerShell:

$bytes = New-Object byte[] 10485760
[System.Security.Cryptography.RandomNumberGenerator]::Create().GetBytes($bytes)
[System.IO.File]::WriteAllBytes("$PWD\random-10mb.bin", $bytes)

Any platform, from a script

Python:

import os

with open("random-10mb.bin", "wb") as f:
    f.write(os.urandom(10 * 1024 * 1024))

Node.js:

import { randomBytes } from 'node:crypto';
import { writeFileSync } from 'node:fs';

writeFileSync('random-10mb.bin', randomBytes(10 * 1024 * 1024));

In an automated test suite, generating large files in a setup step like this is better than committing them to the repository.

Check what you made

Confirm the exact size:

wc -c < random-10mb.bin

On Windows:

(Get-Item .\random-10mb.bin).Length

To see whether a file is sparse on Linux or macOS, compare its apparent size with the space it uses:

ls -l sparse-10mb.bin
du -h sparse-10mb.bin

A sparse 10 MB file shows 10485760 bytes in ls and close to zero in du.

Boundary sizes

To test a limit precisely, make three files: one byte under, exactly on, and one byte over.

LIMIT=5242880
head -c $((LIMIT - 1)) /dev/urandom > under.bin
head -c $LIMIT /dev/urandom > exact.bin
head -c $((LIMIT + 1)) /dev/urandom > over.bin

Remember that an upload request is a little larger than the file it carries. The guide to testing file size limits explains how to account for that.

Text files of a given size

Random bytes are not valid text. For a large text file, repeat a line until the size is reached and cut it at the exact length:

yes 'The quick brown fox jumps over the lazy dog.' | head -c 1048576 > text-1mb.txt

When a generated file is not enough

These commands give you bytes, not formats. A 10 MB file of random data named photo.jpg is not a JPEG: an upload form that validates content will refuse it, and an image library will fail to open it. That is the right tool only when a refusal is what you want to test.

When the application needs to open the file, you need a real one. Padding a small valid file with junk to reach a size is unreliable too, since some parsers ignore trailing bytes and others reject them.

That is the gap the sized samples here fill. Each is a genuine, parseable file built to its exact byte count: a 10 MB PDF with real pages, a 5 MB JPG that decodes to a real image, a 10 MB XLSX with real rows. Each download page explains how the file was constructed.

Files used in this guide

Sample files referred to in this guide
FileFormatSizeContentsDownload
1 MB BIN samplesample-bin-1mb.bin BIN 1 MB1,048,576 bytes Pseudo-random bytes Download BIN
10 MB BIN samplesample-bin-10mb.bin BIN 10 MB10,485,760 bytes Pseudo-random bytes Download BIN
1 MB TXT samplesample-txt-1mb.txt TXT 1 MB1,048,576 bytes Numbered lines: 10,547 Download TXT
Empty file (0 bytes)empty-file.txt TXT 0 B0 bytes Lines: 0 Download TXT

Related guides