truncate and fallocate
Shrink or grow a file to the size you need, create a sparse or preallocated file
truncate changes a file's size: it cuts off the excess and pads the missing part with zeros — but those zeros take no disk space (the file becomes sparse). fallocate (from util-linux), on the contrary, really allocates space for the file. Both work instantly, unlike dd. The sandbox has GNU coreutils 9.4 and util-linux.
Syntax
truncate -s SIZE file…
truncate -r reference file…
fallocate -l LENGTH file
fallocate [-o OFFSET] -l LENGTH {-p|-c|-i|-z} file
fallocate -d file
truncate options
| Option | What it does |
|---|---|
-s SIZE | set or change the size (forms are in the table below) |
-r file | take the size from another file; with -s +N/-s -N — relative to it |
-c | don't create missing files (without -c, truncate creates the file) |
-o | treat SIZE as a number of the file's I/O blocks rather than bytes |
Size forms
| Syntax | Meaning | Example: the file was 5000 bytes |
|---|---|---|
N | exactly N | -s 1000 → 1000 |
+N | grow by N | -s +1K → 6024 |
-N | shrink by N | -s -1000 → 4000 |
'<N' | at most N (shrink if larger) | -s '<2000' → 2000 |
'>N' | at least N (grow if smaller) | -s '>8000' → 8000 |
/N | round down to a multiple of N | -s /4K → 4096 |
%N | round up to a multiple of N | -s %4K → 8192 |
Units: K, M, G, T — powers of 1024; KB, MB, GB — powers of 1000. Quote < and > — otherwise they're shell redirections.
fallocate options
| Option | What it does |
|---|---|
-l LENGTH | how many bytes to allocate (100M, 1G; MiB = M, MB — powers of 1000) |
-o OFFSET | starting from which position in the file (0 by default) |
-n | allocate space but don't change the file's visible size (the file must exist) |
-p | "punch a hole": turn a range into zeros with no disk space (implies -n) |
-c | collapse a range, shifting the rest of the file (the FS must support it; offset and length must be block multiples) |
-i | insert an empty range, shifting the rest forward |
-z | fill a range with zeros, allocating space |
-d | find zero-filled blocks and make them "holes" — shrink the file in place |
-x | use posix_fallocate — works everywhere, but slowly (writes zeros) |
-v | verbose output |
Sparse or allocated
truncate -s 1G f | fallocate -l 1G f | dd if=/dev/zero … count=1024 bs=1M | |
|---|---|---|---|
ls -l (visible size) | 1.0G | 1.0G | 1.0G |
du (disk space) | 0 | 1.0G | 1.0G |
| Speed | instant | instant | writes every byte |
| Space guaranteed | no — may run out while writing | yes | yes |
Compare the visible and real size like this: ls -lh f; du -h f or du -h --apparent-size f.
Examples
# empty a file without deleting it (permissions and owner are kept)
truncate -s 0 app.log
# a 1 GiB placeholder file using no space — for tests
truncate -s 1G big.img
ls -lh big.img; du -h big.img
# reserve 500 MiB for data in advance
fallocate -l 500M data.bin
# a swap file (space must really be allocated, not sparse)
fallocate -l 1G /swapfile && chmod 600 /swapfile
# keep the first 100 bytes
truncate -s 100 file.txt
# grow to a size that is a multiple of 4 KiB (images, alignment)
truncate -s %4K disk.img
# the same size as another file
truncate -r original.bin copy.bin
# turn already written zeros into "holes" and free the space
fallocate -d disk.img
Pitfalls
- Emptying a log that is written without
O_APPEND. If a process opened the file with>(not>>), aftertruncate -s 0it keeps writing at the old offset: null bytes appear at the start of the file and the size returns to what it was. Programs that opened the log in append mode (>>) correctly write from zero. truncatecreates the file if it doesn't exist. A typo in the name gives no error — a new empty file appears. The safeguard is-c.- A sparse file can "inflate".
cp --sparse=never, archivers without hole support, network transfers turn zeros into real bytes, and disk space runs out unexpectedly. -s '<N'without quotes is-splus input redirection from a file namedN.fallocate -n/-pon a nonexistent file — "cannot open … No such file or directory": these modes don't create the file.- Not every FS supports
fallocate(-c,-irequire ext4/xfs). Where it doesn't — "Operation not supported";-x(slow) orddhelps. - Truncation is irreversible. The cut-off data is gone;
truncate -s +Nwon't bring it back — it appends zeros.