
Have you required a test file or files of a specific size? How about needing to quickly allocate space? If so my first recommendation is the fallocate command. In this blog post, we will explore this command, how it differs from other options, and what else it can do.
Introduction
The fallocate command is part of the Linux util-linux package. This is pre-installed on most standard Linux Distributions. It is most commonly used to quickly create blank files. It allows you to pre-allocate space to both preexisting files and create new files while allocating space.
Basic Syntax
The basic syntax of the command is as follows:
fallocate [options] filepath
Considerations
- Files created with the fallocate command are empty files that contain no real data. Though what it creates is not random, if you need truly random files this is not the tool for you.
- Not all file systems support fallocate, though if you stick with a commonly used Linux file system this shouldn’t come up.
- ⚠️ Warning: If you have to use fallocate on exiting files make sure to make a backup before you do in case you need to revert the change.
Flags
Here are the flags I think you will find most useful will be most useful:
-c, --collapse-range remove a range from the file -d, --dig-holes detect zeroes and replace them with holes -i, --insert-range insert a hole at range, shifting existing data -l, --length <num> length of the file (also referred to as size) in bytes unless provided with another unit of size -n, --keep-size prevent file length modification -o, --offset <num> offset for range operations, assumed to be a byte range unless otherwise specified -p, --punch-hole replace a range with a hole (implies -n) -z, --zero-range zero and ensure allocation of a range -v, --verbose verbose mode
If you’re not seeing something here you think it should be able to do please check with the –help flag or refer to the manual page.
Examples
- Create a 100MB File:
fallocate -l 100M newfile
This command creates a new file named newfile and allocates 100MB of disk space to it. As you can see we specified the length in MB rather than specifying length bytes - Make an existing file the size of 1G
fallocate -l 1G existingfile
This command pre-allocates 1G to an existing file without changing the contents. The allocated disk space will now show up under ls for this file. - Allocate space to a file keeping file size
fallocate -n -l 10M existingfile
This will preallocate space of 10M of disk space for existing file without the size shown by ls changing. - Allocate Space with an Offset:
fallocate -o 2M -l 100M newfile
By using the length and offset arguments this command allocates 100MB of disk space for newfile, the specified range starting at an offset of 2MB. - Zero a Range in a File:
fallocate -z -o 5000000 -l 10000000 existingfile
This command zeroes out a 10000000 byte range in existingfile, starting from an offset of 5000000. It should be noted that the byte range starting at 5000000 is equal to 5MB and the length bytes is equal to 10MB.
Alternative Methods
It should be noted that there are alternative methods for creating files for testing. They go about this in different ways so if fallocate doesn’t appear quite what you’re looking for please research other methods before making your decision.
Some of these alternative tools are listed below.
- truncate
- head
- tail
- dd
Conclusion
In this post, we have answered what the fallocate command is. This pre-installed command is most commonly used to quickly create files for testing. Though that isn’t all we will explore everything from Syntax, Considerations, Flags, Examples, and more. In this blog post, we will explore this command, how it differs from other options, and what else it can do.
My Other Posts
Are you done reading and looking for additional material, why not check out my other posts?

Embracing a lifelong passion for technology since childhood, CJ delved into the intricate workings of systems, captivated by the desire to understand the unknown. This innate curiosity led to his discovery of Linux, a revelation that resonated deeply. With more than 7 years of on the job experience, he’s honed his technical skills as a Geek and Senior Linux Systems Administrator.
Leave a Reply