fallocate Command in Linux with 5 Examples

Written by: Subhash Chandra   |   Last updated: October 23, 2023

The fallocate command in Linux allocates or deallocates disk blocks to/from a file while creating the file. It can quickly allocate the required disk space for a file without having to perform time-consuming I/O operations. Unlike traditional methods that involve writing zeros (zero-fill), fallocate updates the file metadata, informing the filesystem of the new file size.

The fallocate command is not commonly used by normal users, however, it is useful for system administrators to build or configure storage space and directory usage monitoring.

Fallocate works much faster compared to dd command for allocating disk space to a file. The preallocation is done quickly by allocating blocks and marking them as uninitialized, requiring no IO to the data blocks.

The file size is measured by the length and offset arguments of the file. Files that you create using fallocate may contain different data on your file system. You can even use fallocate system call to extend the file length on your file system, which will contain zeros.

Syntax

The basic syntax to use fallocate command is as follows:

fallocate [options] [size] file_name

If you get the following error message when you run fallocate, then the utility is not installed on your computer: "fallocate: command not found".

To fix this problem, you must install the fallocate utility to allocate storage space to a file:

sudo apt install util-linux

Create a File of the Specified Size

Use fallocate command with the -l option to create a file of the specified size. For example:

fallocate -l 2GB docs

You can specify two file size options- either in the multiples of 1000 byes or in the multiples of 1024 bytes. For example, if you specify Mega Byte (MB) it is 1000x1000 bytes, or if you specify Mebibyte (MiB or M), it is 1024x1024 bytes.

Length Arguments Suffixes

You must be very careful with the size suffixes for the files you create using fallocate. This command supports KiB or K for 1024 bits, MiB or M for 1024x1024 bytes, and so on. The other suffixes are GiB or G, TiB or T, PiB or P, ZiB or Z. You can also use KB for 1024 bits, MB or 1000x1000 bytes, GB, TB, PB, and ZB.

Fallocate Linux Examples

Fallocate in Linux supports various options that you can use to create files with different options. You can easily create a large file. The following examples show you some of the most common use cases of this command:

1. Using Fallocate to Create a 1GB File

To create a file of the size 1GB using fallocate, specify the 1 GB size followed by the -l option:

fallocate -l 1GB query.png

To verify if the specified size is allocated to the file, use the du -sh command to check the free size of the file:

du -sh query.png
Verify 1GB file created by fallocate

Note: A small amount of storage space might be used by the file metadata and the displayed size may be smaller than the specified size.

2. Using Fallocate to Create a 10GB File

If you want to create an even bigger file, say 10GB, the process remains the same. Just specify the size, which is 10GB in this case:

fallocate -l 10GB users

To verify the allocated storage space and display other file metadata, use the ls command with the -lh option:

ls -lh users
Verify 10GB file created by fallocate

3. Using Fallocate to Create 50MB File

To create a 50MB file using the fallocate command, type:

fallocate -l 50MB dev

To check the size, use the ls -lh command:

ls -lh dev
Verify 50GB file created by fallocate

4. Using Fallocate to Create 100KB File

To create a 100KB file using the fallocate command, type:

fallocate -l 100KB qa

To check the size, use the ls -lh command:

ls -lh qa
Verify 100KB file created by fallocate

5. Using Fallocate to Create Bytes File

You can also specify the size in bytes to create a file using the fallocate command:

fallocate -l 5124789 steve

To verify the size of the created file and to display the size in bytes, type:

ls -l steve
Verify bytes file created by fallocate

fallocate Options

Here are some of the useful fallocate options:

OptionsDescription
--length or -lSpecify the length of the file space that is to be allocated.
-o, --offset=offsetSpecify the offset, the starting point from the beginning of the file, where the operation is to be performed.
-d, --dig-holesDetect and free up any blocks in the file that contain only zeros.
--punch-hole or -pDeallocates blocks by creating hole and filesystem blocks are removed from the file. Must be used with both --length and --offset.
--zero-range or -zZero out a range of a file. Ut doesn't deallocate the space.
--insert-range or -iInserting a zeroed range at a specified offset, shifting existing data to higher offsets

Conclusion

In this tutorial, we learned how to use fallocate to preallocate storage space to a file or deallocate the disk blocks. You can specify the size using various disk space units, including KB, KiB, MB, MiB, GB, or GiB. Use the examples described in this tutorial to create files as per your requirements. To know more about the fallocate command and its options, refer to the fallocate command man page.

About The Author

Subhash Chandra

Subhash Chandra

Subhash Chandra, an Oracle Certified Database Administrator and professional writer, works as a Consulting User Assistance Developer at Oracle, bringing over 15 years of experience to the role. He enjoys sharing his technological passion through how-to articles, simplifying complex concepts in Linux, Windows, Mac OS, and various other platforms and technologies for a wide audience.

SHARE

Comments

Please add comments below to provide the author your ideas, appreciation and feedback.

Leave a Reply

Leave a Comment