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.
fallocatetells 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/zeroreads from the zero device, which produces an endless stream of null bytes.of=testfile.imgwrites to the output file you want to create.bs=1Msets the block size to 1 megabyte per read/write operation.count=100tellsddto copy exactly 100 blocks, giving you100 × 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
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.
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
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.






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