truncate -s: resize files to an exact byte length

Empty log files or preallocate sparse disk images instantly with coreutils truncate on Linux.

LinuxsparsetruncateFilesMedium risk
Run truncate -s 0 FILE to empty it, or truncate -s 10G FILE to extend it to exactly 10 GiB without writing zeros.

01Command

Command
$ truncate -s 0 app.log && truncate -s 10G swapfile.img

02What 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

RiskMedium risk
!Warning: Shrinking silently destroys everything beyond the new size — there is no confirmation and no undelete.

04Undo

✓No undo needed — this command does not change persistent state.

05Sources

  • truncate(1) — Arch Linux manual
    Arch Linux · technical · 2026-09-17
    Evidence: -s SIZE sets the file size exactly; supports K/M/G/T suffixes and +/- relative sizes
  • truncate(2) — Linux manual page
    man7.org · technical · 2026-09-17
    Evidence: 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