← Full reference

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

OptionWhat it does
-s SIZEset or change the size (forms are in the table below)
-r filetake the size from another file; with -s +N/-s -N — relative to it
-cdon't create missing files (without -c, truncate creates the file)
-otreat SIZE as a number of the file's I/O blocks rather than bytes

Size forms

SyntaxMeaningExample: the file was 5000 bytes
Nexactly N-s 1000 → 1000
+Ngrow by N-s +1K → 6024
-Nshrink 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
/Nround down to a multiple of N-s /4K → 4096
%Nround 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

OptionWhat it does
-l LENGTHhow many bytes to allocate (100M, 1G; MiB = M, MB — powers of 1000)
-o OFFSETstarting from which position in the file (0 by default)
-nallocate 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)
-ccollapse a range, shifting the rest of the file (the FS must support it; offset and length must be block multiples)
-iinsert an empty range, shifting the rest forward
-zfill a range with zeros, allocating space
-dfind zero-filled blocks and make them "holes" — shrink the file in place
-xuse posix_fallocate — works everywhere, but slowly (writes zeros)
-vverbose output

Sparse or allocated

truncate -s 1G ffallocate -l 1G fdd if=/dev/zero … count=1024 bs=1M
ls -l (visible size)1.0G1.0G1.0G
du (disk space)01.0G1.0G
Speedinstantinstantwrites every byte
Space guaranteedno — may run out while writingyesyes

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 >>), after truncate -s 0 it 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.
  • truncate creates 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 -s plus input redirection from a file named N.
  • fallocate -n/-p on a nonexistent file — "cannot open … No such file or directory": these modes don't create the file.
  • Not every FS supports fallocate (-c, -i require ext4/xfs). Where it doesn't — "Operation not supported"; -x (slow) or dd helps.
  • Truncation is irreversible. The cut-off data is gone; truncate -s +N won't bring it back — it appends zeros.

See also

dd · df-du · rm · logrotate · stat · split · redirection