The Fallocate Linux Command, And How To Use It To Quickly Create Files For Testing

In this blog post, we will explore this command, how it differs from other options, and what else it can do.

On this page
  1. Introduction
  2. Basic Syntax
  3. Considerations
  4. Flags
  5. Examples
  6. Alternative Methods
  7. Conclusion
  8. My Other Posts

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:

Code
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:

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

  1. Create a 100MB File:fallocate -l 100M newfileThis 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
  2. Make an existing file the size of 1Gfallocate -l 1G existingfileThis 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.
  3. Allocate space to a file keeping file sizefallocate -n -l 10M existingfileThis will preallocate space of 10M of disk space for existing file without the size shown by ls changing.
  4. Allocate Space with an Offset:fallocate -o 2M -l 100M newfileBy using the length and offset arguments this command allocates 100MB of disk space for newfile, the specified range starting at an offset of 2MB.
  5. Zero a Range in a File:fallocate -z -o 5000000 -l 10000000 existingfileThis 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.

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

Are you done reading and looking for additional material, why not check out my other posts?

Illustrated avatar of CJ Saathoff
Written by

CJ Saathoff

CJ Saathoff writes for the NameHero blog.

More from CJ →