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

- Author: [CJ Saathoff](https://www.namehero.com/blog/author/cj-saathoff/)
- Published: 2024-08-22
- Updated: 2024-11-07
- Category: [Linux Command Line](https://www.namehero.com/blog/category/linux-command-line/)
- Web page: https://www.namehero.com/blog/the-fallocate-linux-command/

[![](https://blogcdn.namehero.com/blog/wp-content/uploads/2024/07/31145745/fallocate-1024x576.jpg)](https://blogcdn.namehero.com/blog/wp-content/uploads/2024/07/31145745/fallocate.jpg)

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 &lt;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 &lt;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](https://man.archlinux.org/man/fallocate.1.en).

## Examples

1. **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
2. **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.
3. **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.
4. **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.
5. **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](https://www.namehero.com/blog/linux-file-truncation-truncate-like-a-master/)
- 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](https://www.namehero.com/blog/author/cjthedj97/)?

## Related posts

- [How to Use the Tee Command in Linux](https://www.namehero.com/blog/how-to-use-the-tee-command-in-linux/)
- [How to Use the xargs Command in Linux](https://www.namehero.com/blog/how-to-use-the-xargs-command-in-linux/)
- [Understanding The Mv Command In Linux](https://www.namehero.com/blog/understanding-the-mv-command-in-linux/)
- [How to Use the xxd Command in Linux](https://www.namehero.com/blog/how-to-use-the-xxd-command-in-linux/)
