truncate -s: resize files to an exact byte length
Empty log files or preallocate sparse disk images instantly with coreutils truncate on Linux.
01Command
$ truncate -s 0 app.log && truncate -s 10G swapfile.img02What it does
truncate calls the truncate(2) syscall, so extending a file costs almost nothing — the extra space is sparse holes of zeros that the filesystem materializes only when written. That makes it the instant way to clear a growing log (truncate -s 0 keeps the file's inode open by running processes) or preallocate a 10G swap/VM image in milliseconds. Relative sizes work too: -s +5G grows and -s -1G shrinks by that amount.
03Risk
04Undo
✓No undo needed — this command does not change persistent state.
05Sources
- truncate(1) — Arch Linux manualEvidence: -s SIZE sets the file size exactly; supports K/M/G/T suffixes and +/- relative sizes
- truncate(2) — Linux manual pageEvidence: System call semantics: file is extended with zeros or reduced, losing data beyond the new size
Last verified: 2026-09-17 · Category: Files & Folders
06Related solutions
Find every occurrence of a pattern across all files in a tree, printed as file:line:number matches.
Unpack a gzipped tarball into the current directory while printing each file as it is extracted.
Show total, used, and available space plus usage percentage for every mounted filesystem in human-readable units.