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
|Used 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.
|Deallocates 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.
|Zeroes 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
-z flags are mutually exclusive.
|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.
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.
|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
--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
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.
Đăng ký liền tay Nhận Ngay Bài Mới
Cám ơn bạn đã đăng ký !
Lỗi đăng ký !