Fallocate Command

Linux Fallocate Command Examples

Linux's fallocate command


The "fallocate" command is probably one of the lesser known commands that can be used within Linux to create a file. fallocate is used to preallocate blocks to a file. For filesystems that support the "fallocate" system call, this is done quickly by allocating blocks and marking them as uninitialised, thus requiring no I/O to the data blocks. This is a much faster method of creating a file rather than filling it with zeros. Large files can be created almost instantly without having to wait for any I/O operations to complete. The fallocate system call is supported on the following filesystems: btrfs, ext4, ocfs2, and xfs filesystems (as of kernel version v2.6.31). Below are some examples of how you can use the fallocate command.



Fallocate Command Examples


General Syntax: fallocate [-n] [-o offset] -l length filename

The length and offset arguments may be followed by binary (2^N) suffixes KiB, MiB, GiB, TiB, PiB and EiB (the "iB" is optional, e.g. "K" has the same meaning as "KiB") or decimal (10^N) suffixes KB, MB, GB, PB and EB.


Use fallocate command to create a 1GB file


The following command will allocate a file with a size of 1GB.



fallocate -l 1G test_file1.img

The above command created a file called "test_file1.img" with a size of 1GB. The file was created almost instantly (with no I/O delay).

View of the file created from the above fallocate command:



john@ubuntu01-pc:~$ ls -rtlh test_file*
-rw-r--r-- 1 john john 1.0G Sep  2 12:31 test_file1.img

Use fallocate command to create a 10GB file


The following command will allocate a file with a size of 10GB.



fallocate -l 10G test_file2.img

The above command created a file called "test_file2.img" with a size of 10GB. As with the previous example, the file was created almost instantly (with no I/O delay).

View of files created from the above fallocate commands:



john@ubuntu01-pc:~$ ls -rtlh test_file*
-rw-r--r-- 1 john john 1.0G Sep  2 12:31 test_file1.img
-rw-r--r-- 1 john john  10G Sep  2 12:38 test_file2.img

Note: The exit code returned by the fallocate command is 0 on success and 1 on failure.