How to Create a File of a Specific Size in Linux (fallocate, truncate & dd)
On Linux, you often need to generate a test file of an exact size – say 100 MB or 3 GB – to benchmark network transfer speeds, test disk quotas, create a disk image, or allocate a swap file. Here are the three best commands, how they differ, and why the old dd seek=3G trick sometimes surprises people.

Quick answer: the fastest reliable command on modern Linux (ext4, XFS) is fallocate -l 3G /tmp/testfile – it reserves real disk blocks in a fraction of a second.
Method 1. fallocate – Instant Real Disk Allocation
When you want an actual 3 GB file backed by real filesystem blocks (for swap files or quota tests), use fallocate:
fallocate -l 3G /tmp/testfile ls -lh /tmp/testfile
It accepts standard size suffixes like 500M, 10G, or 1024K, allocating blocks immediately without spending time writing billions of zeroes.
Method 2. truncate – Instant Sparse (“Empty”) File
If you want a file whose apparent size is 3 GB while consuming virtually zero real disk space (a sparse file – handy for virtual disk images), truncate is the cleanest choice:
truncate -s 3G /tmp/testfile
Previously, people built sparse files with a longer dd command:
dd if=/dev/zero of=/tmp/testfile bs=1 count=0 seek=3G
Why readers sometimes reported “this command doesn’t work”: first, minimal or BSD/macOS builds of dd (like BusyBox) do not recognize the 3G suffix in seek and require bs=1M count=0 seek=3072; second, running du -h on a sparse file prints 0 because no physical blocks were written yet (only ls -lh shows the 3 GB logical size).
Method 3. dd – Actually Write Zeroes or Random Data
When you need physical writes to disk (benchmarking real NVMe/SSD throughput, testing network speed, or working on FAT32/NFS where fallocate is unsupported):
# Write 3 GB of zeroes (3072 blocks x 1 MB): dd if=/dev/zero of=/tmp/testfile bs=1M count=3072 status=progress # Write 100 MB of incompressible random data (great for VPN/gzip tests): dd if=/dev/urandom of=/tmp/random100m.bin bs=1M count=100 status=progress
Do not forget to remove the dummy file afterward with rm /tmp/testfile so it does not eat up server space.
If you are creating a large file specifically for Linux swap space, see my guide on adding a swap disk or swap file in Ubuntu. And if you need a fast hourly NVMe box for Linux benchmarks, Fornex (7% off VPS) and Vultr both spin up in seconds (affiliate links).
The rule of thumb is simple: use fallocate -l for instant real space, truncate -s for a zero-cost sparse file, and dd with bs=1M when you actually want bytes written to storage.