Linux truncate Command: Shrink or Extend File Size
This guide explains how to shrink or extend files with Linux truncate, adjust sizes relatively, match a reference file, and distinguish sparse logical size from disk usage. It focuses on practical Linux usage, predictable automation, and the cases where a seemingly small path or option mistake can change the result.
The examples use GNU Coreutils behavior. Check truncate --help, man truncate, and your distribution documentation when portability or a version-specific option matters.
What Is the truncate Command?
truncate shrinks or extends the logical length of files. Shrinking discards bytes beyond the new end. Extending usually creates a sparse region that reads as zero without allocating every block immediately. Missing files are created unless -c is used.
Basic Syntax
truncate [OPTION]... FILE...
Quote paths that contain spaces. When supported, place -- before operands that begin with a hyphen so they are not interpreted as options.
Basic truncate Usage
Reduce a file to zero bytes
The directory entry and attributes remain while the content length becomes zero. Use application-aware rotation rather than truncating active service logs blindly.
truncate -s 0 application.log
Create a file with a specified logical size
Suffixes such as M and GiB define the requested size. The resulting file may be sparse.
truncate -s 100M test.img truncate -s 1GiB sparse.img
Grow or shrink relative to the current size
A leading plus grows the file and a leading minus shrinks it by the specified amount.
truncate -s +10M data.bin truncate -s -512K data.bin
Check the size after clearing the content
truncate usually does not display a message even if it succeeds. Applying -s 0 to an existing file will set its byte size to 0.
printf 'abc' > demo.txt truncate -s 0 demo.txt stat -c '%s %n' demo.txt
An example output is 0 demo.txt. The file content cannot be recovered, so make sure to check backups and target paths before using it on actual logs or data files.
Key Options
| Option | Purpose |
|---|---|
-s SIZE |
Set or adjust the size. |
-c |
Do not create missing files. |
-r FILE |
Use the size of a reference file. |
-o |
Interpret the size in the file's I/O blocks instead of bytes. |
Long options often make reviewed scripts easier to understand. When a script must run on non-GNU systems, verify every non-POSIX option on the target platform.
Practical Examples
Match a reference file's size
The -r option copies only the size, not the contents.
truncate -r reference.img target.img stat -c '%s %n' reference.img target.img
Apply a maximum or minimum size
GNU size prefixes can change a file only when it is above or below a threshold. Quote shell-sensitive prefixes.
truncate -s '<100M' cache.bin truncate -s '>1M' placeholder.bin
Compare logical size and allocated space
An extended area can be a sparse hole. Use stat and du to distinguish logical length from allocated storage.
truncate -s 1G sparse.img stat -c 'size=%s blocks=%b' sparse.img du -h sparse.img
truncate and Related Commands
| Command or method | Primary role | Best fit |
|---|---|---|
truncate |
Change the logical end of a file | Fast resizing and sparse-file creation |
touch |
Create an empty file or change timestamps | Preserving existing size and content |
dd |
Write or copy actual data blocks | Populating contents or imaging devices |
| Log rotation tool | Archive, compress, and coordinate log reopening | Production service logs |
Important Cautions
Shrinking destroys data
Bytes beyond the new end are lost. Work on a backup when the file matters.
Extending does not repair an internal format
Increasing an image or filesystem container's length does not expand its internal structures. Use the format-specific tool.
Sparse files can expand during copying
A copy tool or destination filesystem that does not preserve holes can consume the full logical size.
FAQ
Does truncate -s 0 delete the file?
No. It keeps the file entry and attributes while setting the length to zero.
Does extending a file allocate all of that disk space?
Not necessarily. The extension can be sparse; compare stat and du.
How do I avoid creating a missing file?
Use truncate -c -s SIZE FILE.
Does -r copy data from the reference?
No. It copies only the reference file's size.









