How to Create a Test File of Any Size in Linux

New to Linux commands? Our 100+ Essential Linux Commands course covers everything you used in this guide and a lot more with real examples and explanations.

You need a 1GB test file to benchmark your disk, fill a partition to a specific threshold, or simulate a large log file, and the touch command gives you a zero-byte stub that’s useless for any of those jobs.

The 3 tools that actually do this are dd, fallocate, and truncate, and most people either don’t know all 3 exist or don’t know which one to reach for.

They solve the same problem in completely different ways, and the difference matters depending on what you’re trying to do.

  • dd command writes actual data byte by byte, so the file is fully written to disk before you get your prompt back.
  • fallocate tells the filesystem to reserve the space without writing any data at all, which makes it near-instant even for files measured in gigabytes.
  • truncate sets the file size in metadata only, creating a sparse file where no actual disk blocks are allocated, which makes it the fastest of the 3 but useless for benchmarking.

All 3 commands are standard on any modern Linux system, and all 3 are worth knowing.

Create Large Files with the dd Command

The dd command is a long-standing Unix and Linux utility used for low-level data copying, which means it reads data from an input source and writes it block by block to an output file, which makes it ideal when you need a file filled with actual data.

A common example uses /dev/zero, which continuously supplies null bytes:

dd if=/dev/zero of=testfile.img bs=1M count=100

Example output:

100+0 records in
100+0 records out
104857600 bytes (105 MB, 100 MiB) copied, 0.123456 s, 850 MB/s

Breaking down the command:

  • if=/dev/zero reads from the zero device, which produces an endless stream of null bytes.
  • of=testfile.img writes to the output file you want to create.
  • bs=1M sets the block size to 1 megabyte per read/write operation.
  • count=100 tells dd to copy exactly 100 blocks, giving you 100 × 1MB = 100MB.

The output tells you exactly how many bytes were written and the transfer speed, which is already useful as a rough disk benchmark. The file is real data on disk, not a reservation, so it takes as long as it takes to physically write 100MB to your storage.

To create a 1GB file, you can use:

dd if=/dev/zero of=testfile.img bs=1M count=1024

You can also simplify it like this:

dd if=/dev/zero of=testfile.img bs=1G count=1

For large files, adding status=progress helps monitor progress in real time:

dd if=/dev/zero of=bigfile.img bs=1M count=4096 status=progress
If you test your disk speeds regularly with dd and want to go deeper on Linux storage and I/O, the 100+ Essential Linux Commands course covers the disk tools end to end.

Create Large Files Faster with fallocate Command

The fallocate works at the filesystem level rather than the data level, and it’s the right tool when you need a large file immediately and don’t care what’s in it.

On ext4, xfs, and btrfs filesystems, it pre-allocates disk blocks in a fraction of a second because it’s updating metadata rather than writing actual content.

To create a 1GB file instantly, use the following fallocate command with -l flag that sets the length of the file you want to allocate, and you can use K, M, G, or T as size suffixes.

fallocate -l 1G largefile.img

Verify the file was created with the right size using:

ls -lh largefile.img

Output:

-rw-rw-r-- 1 ravi ravi 1.0G May  7 14:23 largefile.img

That 1.0G confirms the filesystem has reserved exactly 1GB for that file. The key thing to understand is that the file occupies real disk space as far as the filesystem is concerned, but the actual data blocks haven’t been written. If another process tries to write to that space, the filesystem knows it’s already claimed.

If you see fallocate: fallocate failed: Operation not supported, you’re on a filesystem that doesn’t support pre-allocation, like tmpfs or older FAT volumes. In those cases, fall back to dd.

Create Large Files Instantly with truncate Command

The truncate command is the fastest of the 3 because it doesn’t write data or reserve disk blocks at all – it just sets the file size in the filesystem metadata, creating what’s called a sparse file.

To create a 1GB sparse file:

truncate -s 1G sparsefile.img

The -s flag sets the size, and like fallocate, it accepts K, M, G, and T suffixes. But here’s where truncate behaves differently from fallocate.

check the actual disk usage with du:

du -sh sparsefile.img

Output:

0	sparsefile.img

Zero bytes on disk. The file size is metadata only, so no blocks are allocated until something actually writes to the file, which makes truncate useless for disk benchmarking or swap files, but perfect for placeholder files, test fixtures, or any scenario where you just need a file of a specific size without caring about its contents.

Where truncate genuinely stands out is resizing existing files. You can grow or shrink a file without rewriting it, something neither dd nor fallocate does cleanly:

# Grow an existing file by 500MB
truncate -s +500M sparsefile.img

# Shrink a file to exactly 200MB
truncate -s 200M sparsefile.img

Important: shrinking a file with truncate silently discards the data past the new size boundary with no warning, so use it carefully on files that contain real content.

If you’re building a home lab and want to go deeper on Linux storage, the LFCS Certification Course covers sparse files, LVM, and filesystem management.

Verify the Exact File Size

After creating a file with either tool, ls -lh gives you a human-readable size, but for byte-perfect verification you want du or stat, because ls rounds and du reports the actual disk allocation.

stat largefile.img

Output:

File: largefile.img
  Size: 1073741824      Blocks: 2097152    IO Block: 4096   regular file
Device: fd01h/64769d    Inode: 131073      Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1000/    ravi)   Gid: ( 1000/    ravi)
Access: 2024-05-07 14:23:00.000000000 +0530
Modify: 2024-05-07 14:23:00.000000000 +0530
Change: 2024-05-07 14:23:00.000000000 +0530
 Birth: 2024-05-07 14:23:00.000000000 +0530

The Size field shows the exact byte count, and Blocks tells you how many 512-byte blocks are actually allocated on disk.

  • For a fallocate-created file, those blocks are reserved.
  • For a dd-created file, they contain the data you wrote.
  • For a truncate-created file, Blocks will show 0 or near-zero because no real blocks are allocated.

Run du -sh across all 3 to see the difference clearly:

du -sh testfile.img largefile.img sparsefile.img

Output:

100M    testfile.img
1.0G    largefile.img
0       sparsefile.img
If you found this useful, share it with a colleague who’s still using truncate to create test files and wondering why their benchmarks look wrong.

Create a Swap File Properly

One of the most common real-world uses for exact-size files is creating swap space. In this case, the difference between dd and fallocate becomes important.

On ext4 and xfs, both dd and fallocate generally work, but on btrfs, swap files created with fallocate can fail and truncate must never be used for swap files, because the kernel requires swap space to be fully allocated, and a sparse file will cause mkswap to fail or produce an unusable swap area.

The safest and most compatible method is using dd:

sudo dd if=/dev/zero of=/swapfile bs=1M count=2048

Set the correct permissions:

sudo chmod 600 /swapfile

Initialize the swap area:

sudo mkswap /swapfile

Enable the swap file:

sudo swapon /swapfile

To make the swap file persistent across reboots, add this line to /etc/fstab:

/swapfile none swap sw 0 0
If you’re working with disk images, virtual machines, or Linux storage in depth, the LFCS Certification Course on Pro TecMint has a full module on storage management that covers sparse files, LVM, and filesystem tuning.

When to Use dd or fallocate in Linux

The practical decision comes down to what you’re doing with the file afterward and whether you need data or just space.

Use dd when:

  • You need the file filled with actual data (zeros, random bytes from /dev/urandom, or copied data).
  • You’re benchmarking sequential write speed and need real I/O.
  • You’re on a filesystem or environment where fallocate isn’t supported.
  • You need a file with specific content patterns for testing.

Use fallocate when:

  • You need a large placeholder file immediately and disk reservation is the goal.
  • You’re creating a swap file, a loop device image, or a pre-sized log file.
  • Speed matters and your filesystem supports pre-allocation (ext4, xfs, btrfs).

Use truncate when:

  • You need a sparse placeholder file and disk space doesn’t need to be reserved.
  • You need to resize an existing file up or down without rewriting it.
  • You’re creating test fixtures or dummy files for application testing where content doesn’t matter.

One subtle detail: a fallocate-created file on ext4 may behave differently from a dd-created file when you actually write to it later, because the blocks are allocated but uninitialized.

On most production filesystems that’s fine, but for security-sensitive use cases where you need the space zeroed before use, dd with if=/dev/zero is the right choice.

Found the truncate vs fallocate distinction useful? Share this with your team before someone uses the wrong tool for a swap file in production.
Conclusion

You now have 3 reliable ways to create files with an exact size in Linux, and more importantly, you know when to use each one.

  • The dd command writes actual data to disk block by block, which makes it useful for storage benchmarks, zero-filled disk images, swap files, and situations where filesystem compatibility matters.
  • The fallocate command works differently by reserving disk space through the filesystem layer without writing real data, which makes it significantly faster for creating large placeholder files, VM disk images, and test files where the contents do not matter.
  • The truncate command skips both and just sets the file size in metadata, making it instant but sparse, which means no real disk allocation happens until something writes to the file.

The easiest way to understand the difference is to test both commands on your own system.

First, create a 1GB file with fallocate:

time fallocate -l 1G test1.img

Then create the same size file with dd:

time dd if=/dev/zero of=test2.img bs=1M count=1024

On modern filesystems such as ext4 or xfs, the speed difference becomes obvious immediately. You can also compare how disk space is allocated using:

du -sh test1.img test2.img

This gives you a better understanding of how Linux filesystems handle pre-allocation versus physical writes.

In real-world environments, both tools are useful for different tasks. For example, fallocate works well for quickly creating large VM images or temporary test files, while dd is often the better choice for disk benchmarking, swap files, and storage testing where actual data writes matter.

Once you understand how these tools behave, choosing the right one becomes much easier during day-to-day Linux administration.

Root Plan
Premium Linux Education for Serious Learners

Take Your Linux Skills to the Next Level

Root members get full access to every course, certification prep track, and a growing library of hands-on Linux content — with new courses added every month.

What You Get
Ad-free access to all premium articles
Access to all courses: Learn Linux, AI for Linux, Bash Scripting, Ubuntu Handbook, Golang and more.
Access to Linux certifications (RHCSA, RHCE, LFCS and LFCA)
Access new courses on release
Get access to weekly newsletter
Priority help in comments
Private Telegram community
Connect with the Linux community
Ravi Saive
I'm Ravi Saive, an award-winning entrepreneur and founder of several successful 5-figure online businesses, including TecMint.com, GeeksMint.com, UbuntuMint.com, and the premium learning hub Pro.Tecmint.com.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

2 Comments

Leave a Reply
    • @Hai,

      Good point.

      The truncate command is one of the most versatile tools for this task because it can create empty files of any size, expand existing files, or shrink them without needing to rewrite the entire file contents.

      I’ve updated the article to include truncate and also added a comparison between dd, fallocate, and truncate to explain how each one behaves differently when creating large files.

      Reply

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.

Root Plan Premium Linux Education for Serious Learners

Before You Go - Upgrade Your Linux Skills

Root members get everything in one place, with new courses added every month.

What You Get
Ad-free access to all premium articles
Access to all courses: Learn Linux, AI for Linux, Bash Scripting, Ubuntu Handbook, Golang and more.
Linux certifications: RHCSA, RHCE, LFCS and LFCA
Access new courses on release
Weekly newsletter, priority support & Telegram community
Join Root Today and Start Learning Linux the Right Way
Structured courses, certification prep, and a community of Linux professionals - all in one membership.
Join Root Plan →
$8/mo · or $59/yr billed annually