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 2 tools that actually do this are dd and fallocate, and most people either don’t know both 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.

Both commands are standard on any modern Linux system, and both 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.

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.

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 tools generally work, but on btrfs, however, swap files created with fallocate can fail.

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).

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.

Conclusion

You now have two 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 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
  1. Dear Ravie

    Is it secure to use this tool on server,does this pass any information ,like existing vulnerability of current setup and all?

    How can we block these types of scanning on our webserver?Please suggest.

    One more is I am posting almost all your Linux post to my linkedin group FREE LINUX SUPPORT .If you could join that group and share your ideas that would be great for all group members.

    https://www.linkedin.com/groups?home=&gid=4729766&trk=anet_ug_hm

    Regards
    Justin p mathew

    Reply
    • No it will not pass any of your server information on the web. It is very safe to use and detect vulnerabilities of your site. I’ve joined your group and thanks for posting all our articles.

      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