fallocate Command in Linux with Examples

October 12, 2023

Introduction

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

fallocate command in Linux - a tutorial with practical examples.

Prerequisites

  • A system running Linux.
  • Access to the terminal (Ctrl + Alt + T).

fallocate Syntax

The fallocate command takes the following syntax:

fallocate [options] [filename]
  • The [options] are flags that determine the fallocate command behavior.
  • [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.

fallocate Options

The fallocate command accepts flags that further define how it works. The following table shows the key options that fallocate accepts:

Option short formOption long formDescription
-c--collapse-rangeUsed in combination with -o [offset] and -l [length]. Removes a byte range from a file without leaving a hole. The byte range to be collapsed starts at the specified offset and continues for the specified length. It allows you to remove a portion of data from a file, and the file system reclaims the space, ensuring no holes are created.
-p--punch-holeDeallocates space (i.e., creates a hole) in the byte range starting at the specified offset and continuing for the specified length of bytes. Requires the -o [offset] and -l [length] options.
-z--zero-rangeZeroes out the allocated space in the byte range starting at offset and continuing for length bytes. Used in combination with -o [offset] and -l [length]. The -c, -p, and -z flags are mutually exclusive.
-o [offset]--offset [offset]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.
-l [length]--length [length]A mandatory flag that specifies the length (size) of the space to be allocated. The default size is in bytes.
-n--keep-sizeInstructs fallocate to keep the apparent size of the file. Using the flag may allocate blocks of space beyond the end of the file's current size (EOF), which can be later truncated.
-d--dig-holeDeallocates 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 --punch-hole, but it might not be supported on all file systems. Requires the -o [offset] option.

Refer to the section below for practical examples of using the fallocate command.

Note: The default unit for specifying the [length] and [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

Use the -l (--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:

ls -lh
Allocate a specific file size using fallocate.

The output shows that the file has been created and that the size is 1.0 gibibytes.

Allocate Space and Keep File Size

The -n (--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
Allocate space for a file and keep the original data and size.

Running 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

Specify the -o (--offset) and -l (--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
Allocate space to a file in a specific range.

Running the ls -lh command outputs the file details, stating that the file size is 1.1G, which was the intended goal.

Deallocate Space From File

Use the -d (--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.

Conclusion

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.

For more Linux commands, check out our tutorial on the most essential Linux commands or download our Linux commands cheat sheet.

Was this article helpful?
YesNo
Bosko Marijan
Having worked as an educator and content writer, combined with his lifelong passion for all things high-tech, Bosko strives to simplify intricate concepts and make them user-friendly. That has led him to technical writing at PhoenixNAP, where he continues his mission of spreading knowledge.
Next you should read
Linux Network Commands Cheat Sheet
September 13, 2023

Master Linux networking commands! This article provides an overview of 20 Linux network commands, and features a free downloadable PDF cheat sheet.
Read more
30 Bash Commands Cheat Sheet
August 24, 2023

This article is a comprehensive list of the most important Bash commands every user should know. Download a PDF cheat sheet and save the commands for future reference.
Read more
chsh Command in Linux with Examples
August 15, 2023

The chsh command allows users to change the current login shell. This article explains how to change the default shell for current or other users using chsh.
Read more
How to Use sed Command to Delete a Line
July 20, 2023

This article explains how to use sed to delete a line in a text file. See how to use patterns and conditions to delete multiple lines with a single sed command.
Read more