Kiến Thức Linux

fallocate Command in Linux with Examples

fallocate command in Linux - a tutorial with practical examples.

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 form Option long form Description
-c --collapse-range 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.
-p --punch-hole 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.
-z --zero-range 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 -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-size Instructs 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-hole 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 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.

Đăng ký liền tay Nhận Ngay Bài Mới

Subscribe ngay

Cám ơn bạn đã đăng ký !

Lỗi đăng ký !

Add Comment

Click here to post a comment

Đăng ký liền tay
Nhận Ngay Bài Mới

Subscribe ngay

Cám ơn bạn đã đăng ký !

Lỗi đăng ký !