Fallocate Command in Linux with 5 Examples

Channel: Linux
Abstract: ls -lh users 3. Using Fallocate to Create 50MB File To create a 50MB file using the fallocate commandls -lh qa 5. Using Fallocate to Create Bytes File

When you create a new file on your Linux computer, a certain amount of disk space is allocated to the file. Adding new content to this file increases the file size and accordingly, Linux allocates more space to the file. Alternatively, you can use the fallocate command in Linux to preallocate data blocks, which involves allocating the data blocks and marking these data blocks as uninitialized.

In this tutorial, we will learn how to use the fallocate command in Linux to reserve blocks for files.

Fallocate Command

The fallocate command in Linux allocates or deallocates disk blocks to/from a file while creating the file. Fallocate command allocated disk space does not require any I/O to the disk blocks. Preallocation and deallocation of blocks is a way to manipulate how disk space is assigned to a particular file in Linux. You can also use this command to create a swap file or extend the swap 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
Install the Fallocate Utility

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

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
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
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
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
fallocate Options

Here listed some of the useful fallocate options:

OptionsDescription--length or -lTo add the length of the range--insert-range or -iInsert a hole of length bytes--punch-hole or -pDeallocates blocks by creating hole and filesystem blocks are removed from the file.--zero-range or -zZeroes space in the byte range 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.

Ref From: linuxopsys

Related articles