LinuxCommandLibrary

fallocate

Preallocate file space on a filesystem

TLDR

Reserve a file taking up 700 MiB of disk space

$ fallocate [[-l|--length]] [700M] [path/to/file]
copy

Shrink an already allocated file by 200 MiB
$ fallocate [[-c|--collapse-range]] [[-l|--length]] [200M] [path/to/file]
copy

Shrink 20 MB of space after 100 MiB in a file
$ fallocate [[-c|--collapse-range]] [[-o|--offset]] [100M] [[-l|--length]] [20M] [path/to/file]
copy

SYNOPSIS

fallocate [-c|-d|-l length|-n|-o offset|-p|-v|-x|-z] <file>

PARAMETERS

-c, --collapse-range
    Collapse range: shift bytes after offset+length up and deallocate space

-d, --detect-zero-range
    Detect and dig zero-range holes (Linux 4.18+)

-h, --help
    Display help and exit

-l, --length length
    Length for allocation or operation (e.g., 1G, 10M)

-n, --no-hide-stale
    Do not hide stale data during zero-range (with -z)

-o, --offset offset
    Starting offset for operation (default 0)

-p, --punch-hole
    Deallocate space (punch hole) in range

-V, --version
    Display version info and exit

-v, --verbose
    Verbose output

-x, --posix-fallocate
    Use POSIX_FALLOCATE semantics (may zero on error)

-z, --zero-range
    Zero bytes in range without deallocation

DESCRIPTION

fallocate is a Linux command-line utility that preallocates or manipulates disk space for regular files using the fallocate(2) system call. Unlike dd or cp, it avoids writing data blocks, only updating filesystem metadata, making it significantly faster for creating large sparse or fully allocated files.

Default operation preallocates the specified length from offset 0, keeping file size unchanged unless data is written later. It supports advanced modes: punching holes to deallocate space, collapsing or inserting ranges, zeroing ranges, and POSIX-compatible allocation. These are ideal for databases, VM images, benchmarks, or sparse file management.

Requires filesystems like ext4, XFS, Btrfs with extent support; older ones like ext3 have limited functionality. Operations fail gracefully with errors like ENOSPC or ENOTSUP if unsupported.

CAVEATS

Modes like punch-hole/collapse require kernel 2.6.38+ and extent-based filesystems (ext4, XFS, Btrfs); unsupported on ext3/FAT. Fails with ENOSPC/ENOTSUP. Only for regular files, not directories/devices. Length/offset must align with filesystem blocks in some cases.

EXAMPLES

fallocate -l 1G largefile.dat
Preallocate 1 GiB.

fallocate -p file.img -o 100M -l 500M
Punch 500 MiB hole at offset 100 MiB.

fallocate -z -o 0 -l 64K sparsefile
Zero first 64 KiB.

LENGTH SYNTAX

Supports suffixes: K=KiB, M=MiB, G=GiB, T=TiB; or bytes directly.

HISTORY

fallocate(2) syscall added in Linux 2.6.23 (2007). Command introduced in util-linux 2.22 (March 2012), with modes like punch-hole/collapse in later versions (2.23+). Enhanced with detect-zero-range in util-linux 2.32 (2018).

SEE ALSO

dd(1), truncate(1), fallocate(2), posix_fallocate(3)

Copied to clipboard