fallocate command is a utility used to preallocate space for a file. The command is an alternative to creating and filling a file with zeros, as it allows users to quickly allocate space for a file without writing any data to the disk.
fallocate is especially helpful when you want to create files of specific sizes or when you want to prevent disk space from running out before writing a large file.
In this article, you will learn how to use the
fallocate command and see practical, hands-on examples to understand it better.
- A system running Linux.
- Access to the terminal (Ctrl + Alt + T).
fallocate command takes the following syntax:
fallocate [options] [filename]
[options]are flags that determine the
[filename]is the path to the file for which you want to allocate space.
The section below explains the additional options you can use with
fallocate command accepts flags that further define how it works. The following table shows the key options that
|Option short form||Option long form||Description|
|Used in combination with |
|Deallocates space (i.e., creates a hole) in the byte range starting at the specified |
|Zeroes out the allocated space in the byte range starting at |
|An optional flag that allows you to specify an offset within the file where the allocation should start. The default offset unit is bytes. Not providing an offset means that the allocation starts from the beginning of the file.|
|A mandatory flag that specifies the length (size) of the space to be allocated. The default size is in bytes.|
|Deallocates space within a file by creating a hole and attempts to deallocate the space on the disk. It's a more aggressive way to free up space than |
Refer to the section below for practical examples of using the
Note: The default unit for specifying the
[offset] is kilobyte. However, the arguments also accept binary (2^N) suffixes - K (kibibytes), M (mebibytes), G (gibibytes), etc., or decimal (10^N) suffixes KB, MB, GB, PB and EB, to specify larger sizes.
fallocate Command Examples
The following examples show how you can use the
fallocate command to allocate space to a file in Linux.
Allocate Specific File Size
--length) option to allocate a specific size to a file. For example, to allocate 1 GB of space to a file named example.txt, run the following command:
fallocate -l 1G example.txt
Use the ls command to check whether the file has been created with the specified size allocation. Run:
The output shows that the file has been created and that the size is 1.0 gibibytes.
Allocate Space and Keep File Size
--keep-size) option allocates space while retaining the original file size. This option is useful if you want to allocate space but preserve the existing data in the file. For example, to allocate 500M of space and keep the original file data, run:
fallocate -n -l 500M examplefile.txt
ls -lh outputs that the total size of the directory is 501M, and the file size is 6 bytes. Thus, 500M of space was allocated to examplefile.txt while preserving its original size.
Allocate Space in Specific Range
--length) options to allocate space within a specific range of a file. For example, to allocate 100M of space starting at an offset of 1G from the beginning of example.txt, use the following command:
fallocate -o 1G --length 100M example.txt
ls -lh command outputs the file details, stating that the file size is 1.1G, which was the intended goal.
Deallocate Space From File
--dig-hole) option to deallocate space from a file. This option creates a hole in the file, which frees up disk space. For example, to deallocate space at a specific offset, run the following command:
fallocate -d -o 100M --length 200M examplefile.txt
The command creates a hole in examplefile.txt starting at 100 MiB and extending for 200 MiB, freeing up that space on the disk.
This guide demonstrated how to use the
fallocate command to allocate space on the disk for a certain file and showed how to use the command options to further customize how the command works.