How to Create a File of Any Size on Linux and Windows

Written by

in

Creating files with a specific size is useful for testing file uploads, validating storage limits, simulating large datasets, and performing basic disk I/O tests.

Linux commonly uses tools such as dd, truncate, and fallocate. Windows provides the built-in fsutil command, which can create a file with an exact size without requiring third-party software.

This guide explains how to create files of arbitrary sizes on both Windows and Linux.


Creating a File of a Specific Size on Windows

Windows includes the fsutil command-line utility for managing file systems and performing advanced file operations.

To create a file with a specific size, use:

fsutil file createnew <filename> <size-in-bytes>

The size must be specified in bytes.

Example: Create a 500 MiB File

A binary megabyte, more precisely called a mebibyte or MiB, contains 1,048,576 bytes.

Therefore:

500 × 1,048,576 = 524,288,000 bytes

Run the following command in Command Prompt:

fsutil file createnew 500MiB.dat 524288000

Example output:

File 500MiB.dat is created

You can verify the file size with:

dir 500MiB.dat

Administrator Permissions

Depending on the Windows version, destination directory, and security configuration, fsutil may require an elevated Command Prompt.

To run it with administrative privileges:

  1. Open the Start menu.
  2. Search for Command Prompt.
  3. Select Run as administrator.
  4. Execute the fsutil command.

Creating Files with PowerShell

PowerShell also provides convenient ways to create files with exact sizes.

Using SetLength()

The following command creates a 500 MiB file:

$file = [System.IO.File]::Create("500MiB.dat")
$file.SetLength(500MB)
$file.Close()

PowerShell recognizes size suffixes such as:

1KB
1MB
1GB
1TB

In PowerShell, these values are based on powers of 1024. For example:

1MB = 1,048,576 bytes

A safer version that ensures the file handle is closed even if an error occurs is:

$file = $null

try {
    $file = [System.IO.File]::Create("500MiB.dat")
    $file.SetLength(500MB)
}
finally {
    if ($null -ne $file) {
        $file.Dispose()
    }
}

Understanding the File Contents

A file created with fsutil file createnew usually appears empty when opened in a normal text editor. This is because the file does not contain readable text.

When the file is read, its contents are generally returned as null bytes:

00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

In a hexadecimal editor, the beginning of the file may appear similar to:

00000000  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00000010  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00000020  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

A null byte has the hexadecimal value 00. It is not the same as an ASCII space, whose hexadecimal value is 20.

Because text editors do not render null bytes as visible characters, the file may look blank even though it has the requested size.

The file extension does not determine the actual file format. Naming a file 500MiB.zip does not make it a valid ZIP archive. For test files, extensions such as .dat, .bin, or .test are usually clearer.


Creating a File of a Specific Size on Linux

Linux provides several methods for creating files with exact sizes. The best command depends on whether you need to write real data or only reserve a logical file size.


Method 1: Create a Zero-Filled File with dd

The traditional approach uses dd with /dev/zero:

dd if=/dev/zero of=500MiB.dat bs=1M count=500 status=progress

Parameter Explanation

  • if=/dev/zero
    Uses /dev/zero as the input source. It continuously generates null bytes.
  • of=500MiB.dat
    Specifies the output file.
  • bs=1M
    Sets the block size to 1 MiB.
  • count=500
    Writes 500 blocks.
  • status=progress
    Displays progress information while the file is being written.

The resulting file size is:

1 MiB × 500 = 500 MiB

You can verify it with:

ls -lh 500MiB.dat

For the exact byte count, use:

stat --format='%n: %s bytes' 500MiB.dat

Method 2: Create a File Quickly with truncate

If you only need a file with a specific logical size, use truncate:

truncate -s 500M 500MiB.dat

Verify the result:

ls -lh 500MiB.dat

truncate changes the logical file size without necessarily writing data across the entire file. Depending on the file system, the resulting file may be sparse and may consume much less physical disk space than its apparent size.

Compare the logical and physical sizes with:

ls -lh 500MiB.dat
du -h 500MiB.dat
  • ls -lh shows the apparent file size.
  • du -h shows the actual allocated disk space.

Because truncate may create a sparse file, it is generally unsuitable for measuring sequential disk write performance.


Method 3: Allocate Disk Space with fallocate

On supported Linux file systems, fallocate can reserve disk space efficiently:

fallocate -l 500M 500MiB.dat

This is usually much faster than writing 500 MiB of zeroes with dd.

Verify the file:

ls -lh 500MiB.dat
du -h 500MiB.dat

Unlike truncate, fallocate normally allocates physical disk blocks immediately. However, its behavior depends on the file system and storage environment.


Creating Files with Decimal or Binary Units

Storage sizes can be expressed using decimal or binary units.

UnitSize in bytes
1 KB1,000 bytes
1 MB1,000,000 bytes
1 GB1,000,000,000 bytes
1 KiB1,024 bytes
1 MiB1,048,576 bytes
1 GiB1,073,741,824 bytes

Some operating-system tools display binary-sized values using labels such as KB, MB, or GB. Therefore, always check the command’s unit conventions when the exact byte count matters.

Create an Exact 500,000,000-Byte File on Linux

dd if=/dev/zero of=500MB.dat bs=1000000 count=500 status=progress

Alternatively:

truncate -s 500000000 500MB.dat

Create an Exact 500 MiB File

truncate -s 524288000 500MiB.dat

Creating Files Containing Random Data

Zero-filled files compress extremely well and may not accurately simulate real application data.

To create a file containing random data on Linux:

dd if=/dev/urandom of=random-500MiB.dat bs=1M count=500 status=progress

This is useful for:

  • Compression testing
  • Upload testing
  • Backup-system testing
  • Deduplication testing
  • Network-transfer testing

However, generating random data consumes more CPU than reading from /dev/zero.

On Windows, PowerShell can generate random content, but generating hundreds of megabytes cryptographically can be slow and memory-intensive. For large performance-testing workloads, a dedicated benchmarking tool is preferable.


Which Method Should You Use?

Operating systemCommandWrites the entire fileTypical use
Windowsfsutil file createnewNot suitable as a controlled write benchmarkQuickly create an exact-size file
WindowsPowerShell SetLength()NoApplication and file-size testing
Linuxdd if=/dev/zeroYesBasic sequential write testing
LinuxtruncateNoQuickly create a logical-size or sparse file
LinuxfallocateUsually allocates blocks without writing all dataReserve disk space quickly
Linuxdd if=/dev/urandomYesCreate incompressible test data

Important Considerations for Disk I/O Testing

Although dd is frequently used for quick disk tests, it is not a complete storage benchmark.

Results can be affected by:

  • Operating-system page cache
  • File-system caching
  • RAID controller cache
  • Storage-device write cache
  • Compression and deduplication
  • Sparse-file allocation
  • Block size
  • Concurrent workloads
  • Virtual-machine or container storage layers

For a more controlled Linux write test, direct I/O may be used:

dd if=/dev/zero of=test.dat bs=1M count=500 oflag=direct status=progress

Direct I/O support depends on the file system, storage device, alignment, and operating environment.

For serious storage benchmarking, use purpose-built tools such as:

  • fio on Linux and Windows
  • DiskSpd on Windows
  • CrystalDiskMark on Windows

These tools can measure random and sequential workloads, queue depth, latency, IOPS, throughput, and mixed read/write performance more accurately than basic file-creation commands.


Practical Use Cases

Files with predetermined sizes can be used for:

Upload Limit Testing

Verify whether web servers, APIs, reverse proxies, and application frameworks correctly enforce upload-size limits.

Storage Capacity Testing

Confirm that an application handles low-space conditions and large-file operations properly.

Network Transfer Testing

Measure approximate file-transfer speed between systems.

Compression Testing

Compare compression ratios using zero-filled, repeated-pattern, and random-content files.

Backup and Restore Testing

Validate backup software behavior when handling large files.

Application Development

Test progress bars, timeout handling, multipart uploads, checksums, and resumable transfers.


Cleaning Up Test Files

Delete the test file after use to recover disk space.

On Windows:

del 500MiB.dat

In PowerShell:

Remove-Item .\500MiB.dat

On Linux:

rm -f 500MiB.dat

Before creating a large file, check the available disk space to avoid filling the file system.

On Windows:

Get-PSDrive -PSProvider FileSystem

On Linux:

df -h

Conclusion

Windows and Linux both provide built-in tools for creating files with exact sizes.

On Windows, use:

fsutil file createnew 500MiB.dat 524288000

On Linux, use dd when you need to write actual zero-filled data:

dd if=/dev/zero of=500MiB.dat bs=1M count=500 status=progress

Use truncate when only the logical file size matters:

truncate -s 500M 500MiB.dat

Use fallocate when you want to reserve disk space efficiently:

fallocate -l 500M 500MiB.dat

The correct method depends on whether you need an exact logical size, physically allocated storage, real written data, random content, or reliable disk-performance measurements.