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
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
Creates a 1 gigabyte file instantly
Create a 100MB file
Creates a 100 megabyte file
Create swap file
Creates a 2GB swap file
Specify size in bytes
Creates a file with exactly 1,048,576 bytes (1MB)
Create file with offset
Allocates 100MB starting at 50MB offset
Punch holes in file
Creates a hole from 10MB to 30MB in the file
Zero a range in file
Zeros the first 1MB of the file
Keep original file size
Allocates space without changing file size
Verbose allocation
Creates file with verbose output
Create VM disk image
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 |