fallocate Command

The fallocate command preallocates space for files, allowing efficient allocation of disk space without actually writing data. It's much faster than traditional methods like dd for creating large files.

Syntax

fallocate [OPTIONS] -l LENGTH FILENAME

Description

The fallocate command manipulates the allocated disk space for a file, either to preallocate or deallocate it. It's particularly useful for creating large files quickly for testing, swap files, or virtual machine disk images.

Key features:

  • Fast file space allocation without writing data
  • Support for various size units (K, M, G, T)
  • Can deallocate space from files (punch holes)
  • More efficient than dd for creating large files
  • Useful for creating swap files and VM disk images

Common Options

Option Description
-l, --length Specify the length of the range to allocate
-o, --offset Specify the beginning offset of the allocation
-n, --keep-size Don't modify the file size
-p, --punch-hole Deallocate space (punch holes)
-d, --dig-holes Detect and dig holes
-c, --collapse-range Remove a range from the file
-z, --zero-range Zero a range within the file
-v, --verbose Verbose output

Examples

Create a 1GB file

fallocate -l 1G largefile.dat

Creates a 1 gigabyte file instantly

Create a 100MB file

fallocate -l 100M testfile.dat

Creates a 100 megabyte file

Create swap file

sudo fallocate -l 2G /swapfile sudo chmod 600 /swapfile sudo mkswap /swapfile

Creates a 2GB swap file

Specify size in bytes

fallocate -l 1048576 1mb_file.dat

Creates a file with exactly 1,048,576 bytes (1MB)

Create file with offset

fallocate -l 100M -o 50M file_with_offset.dat

Allocates 100MB starting at 50MB offset

Punch holes in file

fallocate -p -o 10M -l 20M largefile.dat

Creates a hole from 10MB to 30MB in the file

Zero a range in file

fallocate -z -o 0 -l 1M file.dat

Zeros the first 1MB of the file

Keep original file size

fallocate -n -l 100M existing_file.dat

Allocates space without changing file size

Verbose allocation

fallocate -v -l 500M verbose_file.dat

Creates file with verbose output

Create VM disk image

fallocate -l 20G vm_disk.img

Creates a 20GB virtual machine disk image

Size Units

Unit Description Example
K Kilobytes (1024 bytes) 100K = 102,400 bytes
M Megabytes (1024² bytes) 100M = 104,857,600 bytes
G Gigabytes (1024³ bytes) 1G = 1,073,741,824 bytes
T Terabytes (1024⁴ bytes) 1T = 1,099,511,627,776 bytes

See also